Do site class sometimes get an activity or something, to let users participate, both to attract users to register, but also improve the user activity of the site. At the same time to participate in the user will receive a certain prize, there are 100% of winning, there is a certain probability of winning, big such as the ipad, iphone5, small in a Q currency or something. Then we will certainly design the algorithm in the program, that is, in accordance with a certain probability for users to receive prizes. Let's take a look at two probability algorithm functions.
Algorithm One
/** * Full Probability calculation * * @param array $p Array (' A ' =>0.5, ' B ' =>0.2, ' C ' =>0.4) * @return string returns the above array of key */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 two
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); return $result; }
The second algorithm is a classical probability algorithm, $PROARR is a pre-set array, assuming that the array is: Array (100,200,300,400), the beginning is from the probability range of 1,1000 to filter whether the first number within the probability range of his occurrence, if not, then the probability of space minus, 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), ' 5 ' = = Array (' ID ' =>6, ' prize ' = ' = ' next time it will be possible in oh ', ' V ' =>50),);
$prize _arr is a two-dimensional array that records all of the prize information for this draw, where the ID indicates 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.
If the winning data is placed in the database, it is necessary to determine the number of winning//in the 1, 2, 3 and other prizes, if the maximum number of unset corresponding awards, avoid repeating the jackpot//code here Eg:unset ($prize _arr[' 0 ') foreach ($prize _arr as $key + $val) { $arr [$val [' id ']] = $val [' V '];} $rid = Get_rand ($arr);//probability of obtaining the prize id$res[' Yes '] = $prize _arr[$rid -1][' Prize '); In the awards//will be excluded from the array, the remaining awards, if the database validation, here can be omitted unset ($prize _arr[$rid-1]); Shuffle ($prize _arr); Scrambled array order for ($i =0; $i <count ($prize _arr); $i + +) { $PR [] = $prize _arr[$i] [' Prize '];} $res [' no '] = $PR; Echo json_ Encode ($res);
Why don't you always get the jackpot?
In many similar sweepstakes, the participants often do not get the jackpot, the author from the point of view of the program to show you, if I was the sponsor of the lottery, I set 6 awards, each prize different winning probability, if the first prize is a high-level sedan, but I set the prize probability of 0, which means what? This means that no matter how much the lottery draws, no one will ever get the premium sedan. When the organizer flips the remaining blocks each time, the participant will find that the first prize may be just under a number in the box next to the draw, blaming their bad luck. Is it really bad luck? In fact, when the participant flipped that block, the program has decided on the award, while flipping through the other blocks to see the prize is just a smoke bomb, confusing the audience and participants. I think after reading this article, you may know that the TV show on the board draw a trick, you probably will never go to the machine to choose color Ball.
PHP several common probabilistic algorithms (sweepstakes, AD preferred)