A lottery small program, introduction can be controlled, but also a prize in front of the display, and the program is simply impossible to obtain! By adding all the probabilities x10, the value of each item in the new array equals its first few and adds itself.
It then generates a number from 0 to the maximum number, loops through it to see which interval it belongs to, and returns the key for that interval.
The code is as follows
$prize = Array (
1 => 3.5,//3.5% probability
2 => 2.5,
3 => 6,
4 => 2,
5 => 42,//42% probability
6 =&G T
7 => 2,
8 => 4,
9 => 2
);
Finally confirm that the addition equals
$prizeList = Array (
1 => Array (' Fairies '),
2 => Array (' 5Q currency '),
3 => Array (' 38888 game Currency '),
4 => Array (' 88888 game currency '),
5 => Array (' Thank you for participating! '),
6 => Array (' Welcome next time! '),
7 => Array (' 10Q currency '),
8 => Array (' 888 Silver dollar Treasure (binding) '),
9 => Array (' Random advanced Mount (Binding) ')
);
Unset ($prize [7]); 10Q This prize directly removed
$times = ten;
$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]);
Based on PHP to achieve a simple lottery small program code for everyone to write so much, but are the core code, I hope to help you learn.