PHP lottery probability algorithm (scratch card, big turntable), Lottery scratch card
This example for everyone to share the probability of PHP winning algorithm, can be used for scraping cards, large turntable and other lottery algorithm, the use is very simple, the code has detailed comments, for your reference, the specific content as follows
<?php/* * Classic probability algorithm, * $PROARR is a pre-set array, * Assuming the array is: Array (100,200,300,400), * Start is to filter from the probability range of 1,1000 the first number is within the probability range of his occurrence, * if not In, 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 range of 1,900. * This filter to the end, 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, the last one must be. * This algorithm is simple and very efficient, * this algorithm is very efficient in large data volume projects. */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; }/* * Awards Array * is a two-dimensional array that records all of the prize information for this draw, * where the ID indicates the winning level, 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), the larger the cardinality, the more accurate the probability. * The sum of V in this example is 100, then the probability of winning the tablet corresponds to 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 ' = ' 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 will be able to in oh ', ' V ' =>50),); /* * Each front page request, PHP Loop Award set Array, * Get the prize ID from the Get_rand by the probability calculation function. * Save the winning prizes in the array $res[' yes ', * while the remaining non-winning information is saved in $res[' no ', * finally output JSON data to the front page. */foreach ($prize _arr as $key + $val) {$arr [$val [' id ']] = $val [' V '];} $rid = Get_rand ($arr); Get the prize ID $res by probability [' yes '] = $prize _arr[$rid -1][' Prize '); Medium Prize unset ($prize _arr[$rid-1]); The prize is excluded from the array and the remaining awards are shuffle ($prize _arr); Scrambled array order for ($i =0; $i
The above is the PHP lottery probability algorithm of all the content, I hope that everyone to learn PHP programming help, but also hope that we support a lot of help the home.
http://www.bkjia.com/PHPjc/1136611.html www.bkjia.com true http://www.bkjia.com/PHPjc/1136611.html techarticle PHP lottery probability algorithm (scraping card, big turntable), lottery scraping card This example for everyone to share the probability of winning PHP algorithm, can be used for scraping cards, large turntable and other lottery algorithm, the use is very simple ...