Home > Backend Development > PHP Tutorial > 还是php的概率的有关问题,求解说

还是php的概率的有关问题,求解说

WBOY
Release: 2016-06-13 12:30:06
Original
704 people have browsed it

还是php的概率的问题,求解说
概率计算函数,网上搜了很多都是这个,不过没看得懂这函数,求解下过程是怎样的~
这数组是id和相应的概率。

<br />
Array<br />
(<br />
????[1] => 1<br />
????[2] => 5<br />
????[3] => 10<br />
????[4] => 12<br />
????[5] => 22<br />
????[6] => 50<br />
)
Copy after login

为什么这样就能得到概率奖项的id呢??get_rand($arr);
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 />
}
Copy after login

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template