Original: http://blog.csdn.net/dongsg11200/article/details/24607877
PHP winning probability algorithm, can be used for scraping cards, large turntable and other lottery algorithm
- <?php
- /*
- * Classical probability algorithm,
- * $PROARR is a pre-set array,
- * Suppose array is: Array (100,200,300,400),
- * Start is to filter the first number from the probability range of 1,1000 within the range of his probability of occurrence,
- * If not, then the probability space, that is, the value of K minus the probability space of the number just now,
- * In this case, minus 100, which means the second number is filtered in the 1,900 range.
- * This filter to the end, there will always be a number to meet the requirements.
- * It's the equivalent of going to a box and touching something,
- * The first is not, the second is not, the third one is not, and the last one must be.
- * This algorithm is simple and highly efficient,
- * The key is that this algorithm has been used in our previous projects, especially in large data volumes, which are very efficient.
- */
- function Get_rand ($proArr) {
- $result = ";
- The total probability accuracy 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;
- }
- /*
- * Awards Array
- * is a two-dimensional array that records all the prize information for this draw,
- * 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 award to 0, which means that the odds of the prize being pumped are 0,
- * The sum of V in the array (cardinality), the larger the cardinality, the more the probability of accuracy can be expressed.
- * 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 it will be able to be 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 winning prizes in array $res[' yes ',
- * While the remaining non-winning information is kept 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 based on probability
- $res [' yes '] = $prize _arr[$rid -1][' Prize ']; //In the awards
- unset ($prize _arr[$rid-1]); //The award is excluded from the array, leaving the award
- Shuffle ($prize _arr); //scrambled array order
- For ($i =0; $i <count ($prize _arr); $i + +) {
- $PR [] = $prize _arr[$i] [' Prize '];
- }
- $res [' no '] = $pr;
- Print_r ($res);
- ?>
PHP Lottery algorithm