PHP implements the big wheel lottery algorithm (code example)

藏色散人
Release: 2023-04-08 09:36:01
forward
3857 people have browsed it

PHP implements the big wheel lottery algorithm (code example)

This articleintroduces you to the PHP language to implement the big carousel lottery algorithm through specific examples. I hope it will help everyone learn PHP lottery Helps.

Process:

1. Assemble the award array;

2. Calculate the probability;

3 .Return the winning status.

The code is as follows: The winning probability 'v' can be set in the background and passed to this method. Pay attention to passing integers

function get_gift(){ //拼装奖项数组 // 奖项id,奖品,概率 $prize_arr = array( '0' => array('id'=>1,'prize'=>'平板电脑','v'=>0), '1' => array('id'=>2,'prize'=>'数码相机','v'=>0), '2' => array('id'=>3,'prize'=>'音箱设备','v'=>0), '3' => array('id'=>4,'prize'=>'4G优盘','v'=>5), '4' => array('id'=>5,'prize'=>'10Q币','v'=>0), '5' => array('id'=>6,'prize'=>'空奖','v'=>5), ); foreach ($prize_arr as $key => $val) { $arr[$val['id']] = $val['v'];//概率数组 } $rid = $this->get_rand($arr); //根据概率获取奖项id $res['yes'] = $prize_arr[$rid-1]['prize']; //中奖项 unset($prize_arr[$rid-1]); //将中奖项从数组中剔除,剩下未中奖项 shuffle($prize_arr); //打乱数组顺序 for($i=0;$i $proCur) { $randNum = mt_rand(1, $proSum); //返回随机整数 if ($randNum <= $proCur) { $result = $key; break; } else { $proSum -= $proCur; } } unset ($proArr); return $result; }
Copy after login

For more related PHP knowledge, please visit

php tutorial

!

The above is the detailed content of PHP implements the big wheel lottery algorithm (code example). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
source:cnblogs.com
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
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!