: This article mainly introduces a simple random lottery program based on PHP. if you are interested in the PHP Tutorial, please refer to it. An introduction to a lottery applet is controllable, and a prize can be displayed on the front end, which is impossible to be obtained at all! After adding all probability x10, the value of each item in the new array is equal to the sum of the first few and the addition of itself.
Then, a number between 0 and the maximum number is generated. you can view the range in a loop and return the key of the range.
The code is as follows:
$ Prize = array (1 => 3.5, // 3.5% probability 2 => 2.5, 3 => 6, 4 => 2, 5 => 42, // the probability of 42% is 6 => 36, 7 => 2, 8 => 4, 9 => 2 ); // finally confirm that the sum is equal to 100 $ prizeList = array (1 => array ('Little fairy '), 2 => array ('5q coin '), 3 => array ('000000'), 4 => array ('000000'), 5 => array ('thank you! '), 6 => array (' welcome to come back next time! '), 7 => array ('10q coin'), 8 => array ('1970 silver ingot (bound) '), 9 => array ('random advanced mounts (bound) '); // unset ($ prize [7]); directly remove the 10Q prize $ times = 10; $ max = 0; foreach ($ prize as $ k =>$ v) {$ max = $ v * $ times + $ max; $ row ['V'] = $ max; $ row ['K'] = $ k; $ prizeZone [] = $ row;} $ max --; // critical value $ rand = mt_rand (0, $ max ); $ zone = 1; foreach ($ prizeZone as $ k = >$ v) {if ($ rand >=$ v ['V']) {if ($ rand >=$ prizeZone [$ k + 1] ['V']) {continue ;} else {$ zone = $ prizeZone [$ k + 1] ['K']; break; }}$ zone = $ v ['K']; break ;} print_r ($ prizeList [$ zone] [0]);
Php-based implementation of simple code for the lucky draw applet is so much written for everyone, but it is all the core code and I hope it will be helpful for everyone to learn.
The above describes how to implement a simple random lottery applet based on PHP, including some content, and hope to be helpful to friends who are interested in PHP tutorials.