A lottery mini program has a controllable introduction, and a certain prize can also be displayed on the front end, but it is impossible to get it in the program! Add all the probability x10 and the value of each term in the new array is equal to the sum of the first few it adds itself.
Then a number between 0 and maximum numbers is generated, and the interval it belongs to is looped to return the key of that interval.
The code is as follows
$prize = array( 1 => 3.5, //3.5% chance2 => 2.5, 3 => 6, 4 => 2, 5 => 42, //42% chance6 => 36, 7 => 2, 8 => 4, 9 => 2 ); //Last confirm that the addition is equal to 100$prizeList = array( 1 => array('Little Fairy'), 2 => array('5Q Coin'), 3 => array('38888 Game Coin'), 4 => array('88888 Game Coin'), 5 => array('Thank you for participating!'), 6 => array('Welcome to come again next time! '), 7 => array('10Q Coin'), 8 => array('888 Silver Ingot (Binding)'), 9 => array('Random Advanced Mount (Binding)') ); //unset($prize[7]); Remove the prize of 10Q directly$times = 10; $max = 0; foreach ($prize as $k => $v) { $max = $v * $times + $max; $row['v'] = $max; $row['k'] = $k; $prizeZone[] = $row; } $max--; //The 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]);
I have written so much for everyone to implement simple lottery mini program codes based on php, but they are all core codes, and I hope they will be helpful to everyone's study and work.