Rumah >pembangunan bahagian belakang >tutorial php > 还是php的概率的有关问题,求解说
还是php的概率的问题,求解说
概率计算函数,网上搜了很多都是这个,不过没看得懂这函数,求解下过程是怎样的~
这数组是id和相应的概率。
<br /> Array<br /> (<br /> ????[1] => 1<br /> ????[2] => 5<br /> ????[3] => 10<br /> ????[4] => 12<br /> ????[5] => 22<br /> ????[6] => 50<br /> )
function get_rand($proArr) { <br /> ????$result = ''; <br /> ????//概率数组的总概率精度 <br /> ????$proSum = array_sum($proArr); <br /> ????//概率数组循环 <br /> ????foreach ($proArr as $key => $proCur) { <br /> ????????$randNum = mt_rand(1, $proSum); <br /> ????????if ($randNum <= $proCur) { <br /> ????????????$result = $key; <br /> ????????????break; <br /> ????????} else { <br /> ????????????$proSum -= $proCur; <br /> ????????} <br /> ????} <br /> ????unset ($proArr); <br /> ????return $result; <br /> }