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, at a glance can understand
--> $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, recording all the lottery award information, * where the ID of the winning rating, prize, said the prize, V to indicate the probability of winning.
* Note that the V must be an integer, you can set the corresponding award of V to 0, which means that the probability of the award is 0, * the sum of V in the array (radix), the larger the cardinality can reflect the accuracy of the probability.
* 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. * */$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 ' =& Gt;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 oh ', ' V ' =>50,);
* * Each front-end page request, PHP cycle Awards set Array, * through the probability calculation function Get_rand get the award ID in the draw. * Save the winning prizes in the array $res[' yes '], * and the remaining outstanding information is saved in $res[' no '], * Finally output the JSON data to the front-end page.
*/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); Scramble Array order for ($i =0; $i