PHP winning probability algorithm, winning probability algorithm _php tutorial

Source: Internet
Author: User

PHP winning probability algorithm, winning probability algorithm


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 the 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

1 functionGet_rand ($PROARR) { 2     $result= ''; 3  4     //the total probability accuracy of probability array5     $proSum=Array_sum($PROARR); 6  7     //probability array Loop8     foreach($PROARR  as $key=$proCur) { 9         $randNum=Mt_rand(1,$proSum); Ten         if($randNum<=$proCur) {  One             $result=$key;  A              Break;  -}Else {  -             $proSum-=$proCur;  the         }  -     }  -     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.

1 $prize _arr Array  2     array3     array4     array5 array     6     array7     array8

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.

1 foreach($prize _arr  as $key=$val) { 2     $arr[$val[' ID ']] =$val[' V ']; 3 } 4  5 $rid= Get_rand ($arr);//get the prize ID based on probability6  7 $res[' yes '] =$prize _arr[$rid-1][' Prize '];//Chinese Awards8 unset($prize _arr[$rid-1]);//The award is excluded from the array, leaving the award9 Shuffle($prize _arr);//Scrambled Array OrderTen  for($i= 0;$i<Count($prize _arr);$i++){  One     $PR[] =$prize _arr[$i[' Prize '];  A }  - $res[' no '] =$PR;  - EchoJson_encode ($res);

http://www.bkjia.com/PHPjc/1001467.html www.bkjia.com true http://www.bkjia.com/PHPjc/1001467.html techarticle PHP winning probability algorithm, winning probability algorithm 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 side ...

  • 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.