Home > Backend Development > PHP Tutorial > Winning probability algorithm for lottery program written in PHP_PHP tutorial

Winning probability algorithm for lottery program written in PHP_PHP tutorial

WBOY
Release: 2016-07-13 09:53:54
Original
815 people have browsed it

Lottery winning probability algorithm written in php

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

 ?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

function get_rand($proArr) {

$result = '';

 

//概率数组的总概率精度

$proSum = array_sum($proArr);

 

//概率数组循环

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

1

2

3

4

5

6

7

8

$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),

);

5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
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;<🎜> <🎜>}<🎜> <🎜>
<🎜> The above code is a classic probability algorithm. $proArr is a preset array. Assume that the array is: array(100,200,300,400). It starts by screening whether the first number is within the probability range of 1,1000. is within the range of occurrence probability. If it is not, then the probability space, that is, the value of k minus the probability space of the number just now, in this case is minus 100, which means that the second number is in 1, Screened within the range of 900. In this way, until the end, there will always be a number that meets the requirements. It's like touching something in a box. If the first one isn't right, the second one isn't right, and the third one isn't right, then the last one must be. This algorithm is simple and very efficient. The key is that this algorithm has been applied in our previous projects, especially in projects with large amounts of data. The efficiency is very good. <🎜> <🎜> Next we configure the awards through PHP. <🎜> <🎜> ?<🎜>
<🎜>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.

 ?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

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

$pr[] = $prize_arr[$i]['prize'];

}

$res['no'] = $pr;

echo json_encode($res);

1

2

3

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

/**

* 抽奖

* @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[] = '谢谢惠顾';

}

shuffle($return);

return $return[array_rand($return)];

}

$data = getReward();

echo $data;

?>

4

5

6

8 9 10 11 12
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 <🎜>$pr[] = $prize_arr[$i]['prize'];<🎜> <🎜>}<🎜> <🎜>$res['no'] = $pr;<🎜> <🎜>echo json_encode($res);<🎜> <🎜>
<🎜> Attached is also a netizen’s implementation method <🎜> <🎜> ?<🎜>
<🎜>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;<🎜> <🎜>?>
The above is the entire content of this article, I hope you all like it. http://www.bkjia.com/PHPjc/1000076.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1000076.htmlTechArticleLottery Program Winning Probability Algorithm Written in PHP 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, and there are detailed comments in the code...
Related labels:
php
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