Home > Article > Backend Development > Analysis of ideas for implementing the lottery probability algorithm function in PHP
So in our work, whether it is a mall, corporate website or other mainstream websites, the company often has many limited-time activities and preferential activities. Of course, the most important one is the lottery, which many companies will use. To attract more customers through lottery activities, today we will tell you about the algorithm of winning probability in this lottery!
First download the PHP winning probability algorithm function library we need to use in this course: http://www.php.cn/xiazai/leiku/773
After the download is completed, find the php class file we need, unzip it to our local directory, and create a new php file!
After completion, we need to call this class in the new php file and instantiate the class:
<?php include_once "codecj.php";//引入类文件 //定义数组 $prize_arr = array( '0' => array('id'=>1,'prize'=>'平板电脑','v'=>1), '1' => array('id'=>2,'prize'=>'数码相机','v'=>5), '2' => array('id'=>3,'prize'=>'音箱设备','v'=>10), '3' => array('id'=>4,'prize'=>'4G优盘','v'=>12), '4' => array('id'=>5,'prize'=>'10Q币','v'=>22), '5' => array('id'=>6,'prize'=>'下次没准就能中哦','v'=>50), ); //每次前端页面的请求,PHP循环奖项设置数组, foreach ($prize_arr as $key => $val) { $arr[$val['id']] = $val['v']; } $rid = get_rand($arr); //根据概率获取奖项id $res['yes'] = $prize_arr[$rid-1]['prize']; //中奖项 unset($prize_arr[$rid-1]); //将中奖项从数组中剔除,剩下未中奖项 shuffle($prize_arr); //打乱数组顺序 for($i=0;$i<count($prize_arr);$i++){ $pr[] = $prize_arr[$i]['prize']; } $res['no'] = $pr; print_r($res); ?>
The running result is as shown below:
Note:
When we do not refresh the page once, the data will change~
The above is the detailed content of Analysis of ideas for implementing the lottery probability algorithm function in PHP. For more information, please follow other related articles on the PHP Chinese website!