An example of the probability algorithm for winning the lottery program in PHP

Source: Internet
Author: User
Tags shuffle
In our daily work, often to some lottery activities, then today we will share with you the probability of PHP winning algorithm, can be used for scraping cards, large turntable and other lottery algorithm. The usage is very simple, the code has the detailed comment explanation, one sees to understand, has the need small partner to refer to.

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);    return $result; }

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),   ' 5 ' = = Array (' ID ' =>6, ' prize ' = ' = ' next time it will be possible in oh ', ' V ' =>50),);

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); The prize ID is obtained according to probability   $res [' yes '] = $prize _arr[$rid -1][' Prize ');  //In the award unset ($prize _arr[$rid-1]);//To remove the prize from the array, The remaining awards are  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);

Another way to implement the last Netizen

/**  * Draw  * @param int $total  *  /function Getreward ($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;  ? >

Summarize:

I believe that through the course of this study, PHP to achieve the lottery winning probability algorithm has a certain understanding, and hope to your work has helped!

Related recommendations:

PHP Lottery Program and random AD implementation algorithm

The core code of the PHP Lottery program (three lucky viewers)

php lottery program probability algorithm

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.