This example for you to share the PHP winning probability algorithm, can be used for scraping cards, large turntable, such as lottery algorithm, the use is very simple, the code has detailed notes for your reference, the specific contents are as follows
<?php/* Classic probabilistic 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 probability range of his occurrence,
* If not, then the probability space, that is, the value of K minus the probability space of just that number, * in this case is minus 100, which means that the second number is filtered in the range 1,900.
* This filter to the end, there will always be a number to meet the requirements.
* It's 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 the large amount of data project.
* * 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;
* * * 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 equipment ', ' V ' =>10), ' 3 ' => ; Array (' ID ' =>4, ' Prize ' => ' 4G usb flash drive ', ' V ' =>12 '), ' 4 ' => array (' ID ' =>5, ' Prize ' => ' 10Q currency ', ' V ' =>22 '), ' 5 ' =&G T
Array (' ID ' =>6, ' Prize ' => ' next time may be able to 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 kept in the $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);
Scrambling array order for ($i =0; $i <count ($prize _arr); $i + +) {$PR [] = $prize _arr[$i] [' Prize '];
$res [' no '] = $PR;
Print_r ($res);
?>
Above is the PHP lottery probability algorithm of the whole content, I hope to learn PHP program to help you, but also hope that we support cloud habitat community.