Then we will inevitably design algorithms in the program, that is, let users win prizes at a certain probability. Let’s first look at two probability algorithm functions.
Algorithm 1
/**
* Full probability calculation
*
* @param array $p array('a'=>0.5,'b'=>0.2,'c'=>0.4)
* @return string Returns the key of the above array
*/
function random($ps){
static $arr = array();
$key = md5(serialize($ps));
if (!isset($arr[$key])) {
$max = array_sum($ps);
foreach ($ps as $k=>$v) {
$v = $v / $max * 10000;
for ($i=0; $i<$v; $i++) $arr[$key][] = $k;
}
}
return $arr[$key][mt_rand(0,count($arr[$key])-1)];
}
Algorithm 2
$result = '';
//Total probability accuracy of 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 pre-set array. Assume that the array is: array(100,200,300,400), and starts by filtering whether the first number is within the probability range of 1,1000. If it is not there, subtract the probability space of the number just now. In this case, subtract 100, which means that the second number is filtered in the range of 1,900. In the end, there will always be a number that meets the requirements. It is equivalent to going to a box to touch something. The first one is not, the second one is not, and the third one is not yet, and the last one must be. This algorithm is simple and very efficient. The key is that this algorithm has been applied in our previous projects, especially in large-scale projects with great efficiency.
Next we configure the award through PHP.
$prize_arr = array(
'0' => array('id'=>1,'prize'=>'tablet','v'=>1),
'1' => array('id'=>2,'prize'=>'digital camera','v'=>5),
'2' => array('id'=>3,'prize'=>'speaker equipment','v'=>10),
'3' => array('id'=>4,'prize'=>'4G USB flash drive','v'=>12),
'4' => array('id'=>5,'prize'=>'10Q coins','v'=>22),
'5' => array('id'=>6,'prize'=>'It may be possible to win next time','v'=>50),
);
Zhong is a two-dimensional array that records all the prize information of this lottery, where id represents the winning level, prize represents the prize, and v represents the winning probability. Note that the v must be an integer. You can set the corresponding award v to 0, which means that the probability of winning the award is 0, and the sum of v in the array (base). The larger the cardinality, the more the accuracy of the probability can be reflected. In this example, the sum of v is 100, so the corresponding winning probability of the tablet is 1%. If the sum of v is 10,000, the winning probability is one in ten thousand.
Each time the front-end page request is requested, the PHP loops the award array and uses the probability calculation function get_rand to get the award id. Save the winning prize in the array $res['yes'], and the remaining unwinned information is saved in $res['no'], and finally output the json number data to the front-end page.
//If you win the 1st, 2nd, and 3rd prizes, if the maximum number is reached, unset the corresponding prize to avoid repeated winnings of the grand prize
//code here eg:unset($prize_arr['0'])
foreach ($prize_arr as $key => $val) {
$arr[$val['id']] = $val['v'];
}
$rid = get_rand($arr); //Get the award id based on probability
$res['yes'] = $prize_arr[$rid-1]['prize']; //Winning Prize
//Remove the winning prize from the array, and the remaining unwinned prizes. If it is database verification, you can save it here
unset($prize_arr[$rid-1]);
shuffle($prize_arr); //Scramble the array order
for($i=0;$i<count($prize_arr);$i++){
$pr[] = $prize_arr[$i]['prize'];
}
$res['no'] = $pr;
echo json_encode($res);
Why can't I win the jackpot?
In many similar lottery activities, participants often cannot win big prizes. The author gives you an example from the perspective of the program. If I am the organizer of the lottery, I have set up 6 prizes, and each prize has a different probability of winning. If the first prize is a luxury sedan, but I have set the probability of winning at 0, what does this mean? This means that no matter how the participants draw, they will never get this luxury sedan. When the organizer flips the remaining blocks every time, the participants will find that the first prize may be under a number next to the block that just won the lottery, and they blame themselves for their bad luck. Are you really lucky? In fact, when the participant flips that block, the program has decided to win the prize, and the prize you see when flipping and looking at other blocks is just a smoke bomb, confusing the audience and participants. I think after reading this article, you may know the trick of flip-flop lottery in TV shows, and you may not be able to choose the Double Color Ball on the machine again.