The lottery probability algorithm of drawing program written by PHP

Source: Internet
Author: User
Tags foreach json shuffle

This article to share is the PHP winning probability algorithm, can be used for scraping cards, large turntable, such as lottery algorithm. The usage is very simple, the code has the detailed annotation explanation, a look is understood, has the need the small partner reference under.

We finish the process of PHP backstage, PHP's main work is responsible for the allocation of prizes and the corresponding probability of winning, the current page click to flip a box will want the background PHP to send Ajax request, then the backstage PHP according to the probability of configuration, through the probability algorithm to give the winning results, The award information is also sent to the front-end page in JSON data format.

First look at the probability calculation function

?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19-20 function Get_rand ($PROARR) {$result = ';   The total probability precision 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 classical probability algorithm, $PROARR is a pre-set array, assuming that the array is: Arrays (100,200,300,400), starting from 1,1000 this probability range to filter the first number within the range of his probability of occurrence, if not, then the probability of space , that is, the value of K minus the probability space of just that number, in this case minus 100, which means the second number is filtered in the range 1,900. In this way, there will always be a number to meet the requirements. The equivalent of going to a box to touch things, the first is not, the second is not, the third is not, the last one must be. This algorithm is simple, and very efficient, the key is that this algorithm has been used in our previous projects, especially the large amount of data in the efficiency of the project is very good.

Next we configure the awards through PHP.

?

1 2 3 4 5 6 7 8 $prize _arr = Array (' 0 ' => array (' ID ' =>1, ' prize ' => ' tablet computer ', ' 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 drive ', ' V ' =>12 ', ' 4 ' => array (' ID ' =>5, ' Prize ' => ' 10Q currency ', ' V ' =>22 '), ' 5 ' => array (' ID ' =>6, ' prize ' = > ' next time may be in the oh ', ' V ' =>50);

The two-dimensional array, recording all the Lottery award information, where the ID indicates the winning level, prize, said the prize, V to indicate the probability of winning. Note that V must be an integer, you can set the corresponding award of V to 0, meaning that the probability of the award is 0, the sum of V in the array (radix), the larger the cardinality of the probability of accuracy. In this case the sum of V is 100, then the tablet computer corresponding to the probability of winning is 1%, if the sum of V is 10000, the probability of winning is one out of 10,000.

Each front-end page is requested, the PHP Loop awards are set array, and the probability calculation function Get_rand Gets the award ID drawn in. The winning prizes are saved in the array $res[' yes ', while the remaining outstanding information is kept in $res[' no ', and the JSON data is then output to the front page.

?

1 2 3 4 5 6 7 8 9 10 11 12 13-14 foreach ($prize _arr as $key => $val) {$arr [$val [' id ']] = $val [' V '];} $rid = Get_rand ($arr); To obtain the award ID $res [' yes '] = $prize _arr[$rid -1][' Prize '); Award unset ($prize _arr[$rid-1]); Remove the award from the array, leaving the award shuffle ($prize _arr); Scrambling 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 previous user

?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 /** * @param int $total/function Getreward ($total =1000) {$win 1 = floor ((0.12* $total)/100); $win 2 = Floor (3* $tota L)/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 patronage ';} shuffle ($return); return $return [Array_rand ($return)]; } $data = Getreward (); echo $data;?>

The above mentioned is the entire content of this article, I hope you can enjoy.

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.