This article shares with you the php winning probability algorithm, which can be used for scratch cards, big roulette and other lottery algorithms. The usage is very simple. There are detailed comments in the code, which can be understood at a glance. Friends who need it can refer to it.
We first complete the backend PHP process. The main job of PHP is to configure the awards and the corresponding winning probability. When the front-end page clicks to flip a certain box, it will send an ajax request to the backend PHP. Then the backend PHP will, based on the configured probability, The winning results are given through the probability algorithm, and the unwon prize information is also sent to the front-end page in JSON data format.
Let’s first look at the probability calculation function
?
2 3 4
|
function get_rand($proArr) { $result = ''; //Total probability accuracy of probability array $proSum = array_sum($proArr); //Probability array loop foreach ($proArr as $key => $proCur) { $randNum = mt_rand(1, $proSum); if ($randNum <= $proCur) {<🎜> <🎜>$result = $key;<🎜> <🎜>break;<🎜> <🎜>} else {<🎜> <🎜>$proSum -= $proCur;<🎜> <🎜>}<🎜> <🎜>}<🎜> <🎜>unset ($proArr);<🎜> <🎜> <🎜> <🎜>return $result;<🎜> <🎜>}<🎜> <🎜> |
<🎜>1<🎜> <🎜>2<🎜> <🎜>3<🎜> <🎜>4<🎜> <🎜>5<🎜> <🎜>6<🎜> <🎜>7<🎜> <🎜>8<🎜> | <🎜> <🎜>$prize_arr = array(<🎜> <🎜>'0' => array('id'=>1,'prize'=>'tablet','v'=>1), '1' => array('id'=>2,'prize'=>'Digital Camera','v'=>5), '2' => array('id'=>3,'prize'=>'Speaker device','v'=>10), '3' => array('id'=>4,'prize'=>'4G USB flash drive','v'=>12), '4' => array('id'=>5,'prize'=>'10Q coins','v'=>22), '5' => array('id'=>6,'prize'=>'You might win next time','v'=>50), ); |
This two-dimensional array records all prize information for this lottery, where id represents the winning level, prize represents the prize, and v represents the probability of winning. Note that v must be an integer. You can set the v of the corresponding award to 0, which means that the probability of winning the award is 0. The sum of v in the array (base). The larger the base, the more accurate the probability can be reflected. . In this example, the sum of v is 100, then the probability of winning for the tablet is 1%. If the sum of v is 10,000, the probability of winning is one in ten thousand.
Every time the front-end page is requested, PHP loops through the award setting array, and obtains the drawn award id through the probability calculation function get_rand. Save the winning prizes in the array $res['yes'], and save the remaining non-winning information in $res['no'], and finally output the json number data to the front-end page.
?
2 3
4 56 13 14 |
foreach ($prize_arr as $key => $val) {
$arr[$val['id']] = $val['v'];
}
$rid = get_rand($arr); //Get the award id based on probability
$res['yes'] = $prize_arr[$rid-1]['prize']; //Win the prize
unset($prize_arr[$rid-1]); //Remove the winning prizes from the array, leaving the unwon prizes
shuffle($prize_arr); //Shuffle the order of the array
for($i=0;$i |
<🎜>1<🎜> <🎜>2<🎜> <🎜>3<🎜> <🎜>4<🎜> <🎜>5<🎜> <🎜>6<🎜> <🎜>7<🎜> <🎜>8<🎜> <🎜>9<🎜> <🎜>10<🎜> <🎜>11<🎜> <🎜>12<🎜> <🎜>13<🎜> <🎜>14<🎜> <🎜>15<🎜> <🎜>16<🎜> <🎜>17<🎜> <🎜>18<🎜> <🎜>19<🎜> <🎜>20<🎜> <🎜>21<🎜> <🎜>22<🎜> <🎜>23<🎜> <🎜>24<🎜> <🎜>25<🎜> <🎜>26<🎜> <🎜>27<🎜> <🎜>28<🎜> <🎜>29<🎜> <🎜>30<🎜> <🎜>31<🎜> <🎜>32<🎜> <🎜>33<🎜> | <🎜> <🎜>/**<🎜> <🎜>* Lucky Draw<🎜> <🎜>* @param int $total<🎜> <🎜>*/<🎜> <🎜>function getReward($total=1000)<🎜> <🎜>{<🎜> <🎜>$win1 = floor((0.12*$total)/100);<🎜> <🎜>$win2 = floor((3*$total)/100);<🎜> <🎜>$win3 = floor((12*$total)/100);<🎜> <🎜>$other = $total-$win1-$win2-$win3;<🎜> <🎜>$return = array();<🎜> <🎜>for ($i=0;$i<$win1;$i )<🎜> <🎜>{<🎜> <🎜>$return[] = 1;<🎜> <🎜>}<🎜> <🎜>for ($j=0;$j<$win2;$j )<🎜> <🎜>{<🎜> <🎜>$return[] = 2;<🎜> <🎜>}<🎜> <🎜>for ($m=0;$m<$win3;$m )<🎜> <🎜>{<🎜> <🎜>$return[] = 3;<🎜> <🎜>}<🎜> <🎜>for ($n=0;$n<$other;$n )<🎜> <🎜>{<🎜> <🎜>$return[] = 'Thank you for your patronage';<🎜> <🎜>}<🎜> <🎜>shuffle($return);<🎜> <🎜>return $return[array_rand($return)];<🎜> <🎜>}<🎜> <🎜>$data = getReward();<🎜> <🎜>echo $data;<🎜> <🎜>?> |