PHP probabilistic algorithm (RPM)

Source: Internet
Author: User

Cases:
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; }

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

$prize _arr = Array (' 0 ' = = Array ('     id ' =>1, ' prize ' = ' computer ', ' V ' =>1),     ' 1 ' = = Array (' ID ' =>2, ' Prize ' + ' camera ', ' V ' =>5),     ' 2 ' = = Array (' ID ' =>3, ' prize ' = ' Speaker ', ' V ' =>10),     ' 3 ' = = Array (' id ' = >4, ' prize ' = ' USB ', ' V ' =>12, '     4 ' = = Array (' ID ' =>5, ' prize ' = ' Q ', ' V ' =>22),     ' 5 ' = Array (' ID ' =>6, ' prize ' = ' refueling ', ' V ' =>50),);  

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);   

PHP probabilistic algorithm (RPM)

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.