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 that records all the awards information for this lottery,
* The ID indicates the winning grade, prize the prize, v indicates the probability of winning.
* Note that 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 (cardinality), the larger the cardinality can reflect the accuracy of the probability.
* In this case the sum of V is 100, then the tablet computer's winning probability 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 ' =>4, ' Prize ' => ' 4G flash drive ', ' V ' =>12),
' 4 ' => array (' ID ' =>5, ' Prize ' => ' 10Q ', ' V ' =>22),
' 5 ' => array (' ID ' =>6, ' prize ' => ' will be able to be in the next time Oh ', ' V ' =>50),
);
/*
* Each front page request, PHP Loop Awards set Array,
* Through the probability calculation function Get_rand get the prize ID in the draw.
* Save the winning prizes in the array $res[' yes '],
* and the rest of the outstanding information is kept 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); Get the award ID based on probability
$res [' yes '] = $prize _arr[$rid -1][' Prize ']; Award in
unset ($prize _arr[$rid-1]); Remove the award from the array, leaving the award in the middle
Shuffle ($prize _arr); Scrambling array Order
for ($i =0; $i