An introduction to a lucky draw applet is controllable, and a prize can be displayed on the front end, which is impossible to be obtained in the program. after adding all the probability x10, the value of each item in the new array is equal to the sum of the first few items plus itself, and then a number between 0 and the maximum number is generated... an introduction to a lucky draw applet is controllable, and a prize can be displayed on the front end, which is impossible to be obtained in the program. after adding all the probability x10, the value of each item in the new array is equal to the sum of the first few items plus itself, and then a number between 0 and the maximum number is generated. you can view the range in a loop, return the key of the interval. the code is as follows:
3.5, // 3.5% probability 2 => 2.5, 3 => 6, 4 => 2, 5 => 42, // 42% probability 6 => 36, 7 => 2, 8 => 4, 9 => 2); // finally confirm that the sum is equal to 100 $ prizeList = array (1 => array ('fairy '), 2 => array ('5q coin '), 3 => array ('000000'), 4 => array ('000000 '), 5 => array ('thank you for your participation! '), 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']) {co Ntinue;} else {$ zone = $ prizeZone [$ k + 1] ['K']; break; }}$ zone = $ v ['K']; break ;} print_r ($ prizeList [$ zone] [0]);?>