We first completed the background PHP process, the main work of PHP is responsible for the allocation of prizes and the corresponding probability of winning, the current page click to flip a block will want to background PHP to send AJAX requests, then the background PHP based on the probability of configuration, through the probability algorithm to give the winning results, At the same time, the winning information is sent to the front page in JSON data format.
Let's look at the probability calculation function
function Get_rand ($PROARR) { $result = '; The total probability accuracy of the 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);
The above code is a classical probability algorithm, $PROARR is a pre-set array, assuming that the array is: Array (100,200,300,400), the beginning is the probability range from 1,1000 to filter whether the first number within the probability range of his occurrence, if not, then the probability space , that is, the value of K minus the probability space of the first number, in this case, minus 100, that is, the second number is filtered within the 1,900 range. In this way, there will always be a number to meet the requirements. is equivalent to go to a box to touch things, the first is not, the second is not, the third is not, then the last one must be. The algorithm is simple and very efficient, and the key is that it has been used in our previous projects, especially in large data volumes.
Next we configure the awards 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 device ', ' V ' =>10), ' 3 ' = = Array (' Id ' =>4, ' prize ' = ' 4G usb ', ' V ' =>12, ' 4 ' = = Array (' ID ' =>5, ' prize ' = ' 10Q ', ' V ' =>22),
The two-dimensional array, which records all the prize information of the lottery, where the ID represents the winning grade, prize represents the prize, and V indicates the probability of winning. Note that the V must be an integer, and you can set the V of the corresponding prize to 0, meaning that the odds of the prize being pumped are 0, the sum of V in the array (cardinality), and the larger the cardinality, the more accurate the probability. In this case, the sum of V is 100, then the winning probability of the tablet computer is 1%, if the sum of V is 10000, then the probability of winning is one out of 10,000.
Every time a front-end page is requested, the PHP loop prizes are set in an array, using the probability calculation function Get_rand to get the prize ID in the drawing. Save the winning prize in the array $res[' yes ', and the remaining non-winning information is saved in $res[' no ', and finally output JSON data to the front page.
foreach ($prize _arr as $key = + $val) {$arr [$val [' id ']] = $val [' V ']; } $rid = Get_rand ($arr); Get the prize ID $res by probability [' yes '] = $prize _arr[$rid -1][' Prize '); Medium Prize unset ($prize _arr[$rid-1]); The prize is excluded from the array and the remaining awards are shuffle ($prize _arr); Scrambled array order for ($i =0; $i
Additional user's implementation method
/** * Sweepstakes * @param int $total */function Getrewar D ($total =1000) {$win 1 = floor ((0.12* $total)/100); $win 2 = Floor (($total)/100); $win 3 = Floor ((12* $total)/100); $other = $total-$win 1-$win 2-$win 3; $return = Array (); for ($i =0; $i < $win 1; $i + +) {$return [] = 1; } for ($j =0; $j < $win 2; $j + +) {$return [] = 2; } for ($m =0; $m < $win 3; $m + +) {$return [] = 3; } for ($n =0; $n < $other; $n + +) {$return [] = ' Thank you for your patronage '; } shuffle ($return); return $return [Array_rand ($return)]; } $data = Getreward (); Echo $data;
The above is the whole content of this article, I hope you can enjoy.