An example of PHP probabilistic algorithm for random ads in lottery programs

Source: Internet
Author: User

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% Winning, there are a certain probability of winning, large such as the ipad,iphone5, What's a small, Q -coin?

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

The code is as follows :

/**

* 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 two

The code is as follows : function Get_rand ($PROARR) {

$result = ";

the 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 array (100,200,300 400) , starting from the 1,1000 k 1 , 900 This range is filtered. 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 .

The code is as follows :

$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 ' , ' V ' =>12),

' 4 ' = = Array (' ID ' =>5, ' prize ' = ' 10Q ' , ' V ' =>22),

' 5 ' = = Array (' ID ' =>6, ' prize ' = ' = ' next time it will be able to be in oh ', ' V ' =>50),

);

is a two-dimensional array that records all the prize information for theIDindicates the winning level,Prizerepresents a prize,vindicates the probability of winning. Note that one of thevmust be an integer, you can match the prize to thevSet as0, which means that the odds of the prize being drawn are0, in the arrayvsum (cardinality), the larger the cardinality, the greater the probability of accuracy can be reflected. In this examplevthe sum of -, then the probability of winning the tablet's match is1%, ifvthe sum is10000, the probability of winning is one out of 10,000.

Every time a front-end page is requested, thePHP 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 unused information is saved in the $res [' No '] and the final output JSON number of data to the front page.

The code is as follows :// If the winning data is in the database, you need to make a judgment on the number of winners

in the 1,2,3 and Other awards, if the maximum number of unset the corresponding award To 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); get the prize ID based on probability

$res [' yes '] = $prize _arr[$rid -1][' Prize ']; Chinese Awards

The award is 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 can't I get the jackpot?

In many similar sweepstakes, the participants often do not win 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 up 6 Awards, each prize different winning probability, if the first prize is a high-class sedan, but I set the probability of the award for 0, what does that mean? 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.

An example of PHP probabilistic algorithm for random ads in lottery programs

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.