首頁 >php教程 >php手册 >php抽奖概率|php概率实现|ProbobAward

php抽奖概率|php概率实现|ProbobAward

WBOY
WBOY原創
2016-06-06 19:38:551491瀏覽

$award = array( 1 = 1, 2 = 3, 3 = 6, 4 = 9, 5 = 12, 6 = 22, 7 = 30, 8 = 50, 9 = 60, 10 =99 ); echogetProbobAward($award); 无 ?phpdefined('ROOT') or exit('Access Denied');class Probob{public function getProbobAward($array){$Sum = array_sum($

$award = array(
1 => 1,
2 => 3,
3 => 6,
4 => 9,
5 => 12,
6 => 22,
7 => 30,
8 => 50,
9 => 60,
10 => 99
);
echo getProbobAward($award);
<?php
defined('ROOT') or exit('Access Denied');

class Probob{
	
	public function getProbobAward($array){
		$Sum = array_sum($array);
		foreach($array as $key => $prob){
			$Rand = mt_rand(1, $Sum);
			if($Rand <= $prob){
				return $key;
			}else{
				$Sum -= $prob;
			}
		}
	}
		
}
陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn