PHP winning probability algorithm, can be used for scraping cards, large turntable and other lottery algorithm. The usage is very simple, the code has the detailed comment explanation, can understand at a glance
Php/** Classical probability algorithm, * $PROARR is a pre-set array, * Assuming array is: Array (100,200,300,400), * Start is to filter from 1,1000 this probability range of whether the first number within the probability range of his occurrence, * if not, then the probability Space, which is the value of K minus the probability space of the first number, * in this case, minus 100, which means that 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. * 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, * The key is that this algorithm has been used in our previous projects, especially the large data volume of the project is very efficient. */functionGet_rand ($PROARR) { $result= ''; //the total probability accuracy of probability array$proSum=Array_sum($PROARR); //probability array Loopforeach($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 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 usb ', ' V ' =>12), ' 4 ' =Array(' id ' =>5, ' prize ' = ' 10Q ', ' V ' =>22), ' 5 ' =Array(' id ' =>6, ' prize ' = ' = ' 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 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 based on probability$res[' yes '] =$prize _arr[$rid-1][' Prize '];//Chinese Awardsunset($prize _arr[$rid-1]);//The award is excluded from the array, leaving the awardShuffle($prize _arr);//Scrambled Array Order for($i= 0;$i<Count($prize _arr);$i++){ $PR[] =$prize _arr[$i[' Prize ']; } $res[' no '] =$PR; Print_r($res);
The above introduces the probability of winning algorithm (PHP can be used for scraping cards, large turntable and other lottery algorithm), including the content of the aspects, I hope that the PHP tutorial interested in a friend helpful.