Home> php教程> PHP源码> body text

PHP抽奖程序概率算法

PHP中文网
Release: 2016-05-23 16:36:46
Original
1458 people have browsed it

跳至

//概率算法,6个奖项 $prize_arr = array( '0' => array('id'=>1,'prize'=>'iphone6','v'=>1), '1' => array('id'=>2,'prize'=>'数码相机','v'=>5), '2' => array('id'=>3,'prize'=>'音箱设备','v'=>10), '3' => array('id'=>4,'prize'=>'50Q币','v'=>24), '4' => array('id'=>5,'prize'=>'10Q币','v'=>60), '5' => array('id'=>6,'prize'=>'1Q币','v'=>1900), ); //每个奖品的中奖几率,奖品ID作为数组下标 foreach($prize_arr as $val){ $item[$val['id']] = $val['v']; } function get($item){ //中奖概率基数 $num = array_sum($item);//当前一等奖概率1/2000 foreach($item as $k=>$v){ //获取一个1到当前基数范围的随机数 $rand = mt_rand(1,$num); if($rand <= $v){ //假设当前奖项$k=2,$v1900,则没中六等奖,总获奖基数2000-1900,前五次循环都没中则2000-1-5-10-24-60=1900,必中6等奖,哈哈 $num -= $v; } } return $res; } $res = get($item); $h = $prize_arr[$res-1]['prize']; echo $h.'; ';
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 Recommendations
    Latest Downloads
    More>
    Web Effects
    Website Source Code
    Website Materials
    Front End Template
    About us Disclaimer Sitemap
    php.cn:Public welfare online PHP training,Help PHP learners grow quickly!