The algorithm for winning the lottery program written in php. Php lottery program winning probability algorithm this article will share with you the php lottery probability algorithm, which can be used for scratch cards, big turntable and other lottery algorithms. The usage is very simple. the code contains detailed comments to the lottery program winning probability algorithm written in php.
This article will share with you the php winning probability algorithm, which can be used for scratch cards, big turntable, and other lottery algorithms. The usage is very simple. the code has a detailed description, which can be understood at a glance. For more information, see.
First, complete the PHP process in the background. The main task of PHP is to configure the awards and the corresponding winning probability. when the front-end page clicks a box, you will want PHP to send ajax requests in the background, then, based on the configuration probability, the backend PHP uses the probability algorithm to give the winning result, and sends the unwon award information to the front-end page in JSON format.
Let's 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 = ''; // The total probability precision of the 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 ), in the beginning, the probability range is used to determine whether the first number is within the probability range of occurrence. if not, the probability space is used, that is, the probability space of the value of k minus the number just now. In this example, we subtract 100, that is, the second number is filtered within the range of 1,900. In this way, there will always be a number that meet the requirements. It is equivalent to touching something in a box. The first one is not, the second is not, and the third is not. The last one must be. This algorithm is simple and highly efficient. The key is that it has been used in our previous projects, especially in projects with large data volumes.
Next we will use the PHP configuration award.
?
1 2 3 4 5 6 7 8 |
$ Prize_arr = array ( '0' => array ('id' => 1, 'prize' => 'tablet PC', '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 disks', 'V' => 12 ), '4' => array ('id' => 5, 'prize' => '10q coin ', 'V' => 22 ), '5' => array ('id' => 6, 'prize' => 'Next time you may be able to get in, ', 'V' => 50 ), ); |
The two-dimensional array records all the prize information for this lottery. the id indicates the prize level, the prize indicates the prize, and the v indicates the probability of the prize. Note that the value of v must be an integer. you can set the value of v to 0, which means that the probability of the award is 0, and the sum (base) of the value of v in the array ), the larger the base, the more accurate the probability is. In this example, the sum of v is 100, and the probability of winning a tablet is 1%. if the sum of v is 10000, the probability of winning a tablet is one in ten.
Each request on the front-end page sets an array of PHP loop awards. the probability calculation function get_rand is used to obtain the prize id. Save the prize to the array $ res ['yes'], and save the remaining unwon information in $ res ['no, finally, the number of json data is output 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); // Obtain the award id based on the probability. $ Res ['yes'] = $ prize_arr [$ rid-1] ['prize']; // Medium Award Unset ($ prize_arr [$ rid-1]); // removes the Medium Award from the array, leaving the remaining ones. Shuffle ($ prize_arr); // disrupt the array order For ($ I = 0; $ I $ Pr [] = $ prize_arr [$ I] ['prize']; } $ Res ['no'] = $ pr; Echo json_encode ($ res ); |
See the implementation method of a netizen.
?
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 all the content of this article. I hope you will like it.
Tips this article will share with you the php winning probability algorithm, which can be used for scratch cards, big turntable, and other lottery algorithms. The usage is very simple. the code contains detailed comments...