PHP winning probability algorithm, can be used for scratch card, large turntable lottery algorithm. The usage is very simple, there are detailed comments in the code, you can understand
<!--?</span--> PHP
/ *
* classical probability algorithms,
* $proArr is a preset array,
* array(100,200,300,400),
* start by screening if the first number is within the probability range of 1,1000,
* if not, then the probability space, which is the value of k minus the probability space of that number,
* in this case, it's minus 100, which means the second number is filtered in the range of 1,900.
* in this way, in the end, there will always be a number that meets the requirements.
* is the equivalent of going into a box to touch something,
* the first one is not, the second one is not, the third one is not, then the last one must be.
* this algorithm is simple and very efficient,
* the point is that this algorithm has been used in our previous projects, especially in projects with large data volumes.
* /
The function get_rand ($proArr) {
$result = ' ';
The total probability precision of a 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;
}
}
The unset ($proArr);
Return the $result;
}
/ *
* awards array
* is a two-dimensional array that records all the prize information of the lottery.
* where id represents the winning grade, prize represents the prize, and v represents the probability of winning.
* note that the v must be an integer. You can set the v of the corresponding prize to 0, which means the probability of winning the prize is 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, so the probability of winning the lottery for a tablet is 1%.
* if the sum of v is 10,000, then the probability of winning is one in 10,000.
*
* /
$prize_arr = array (
'0' => array('id'=>1,'prize'=>' tablet ','v'=>1),
Array ('id'=>2,'prize'=>' digital camera ','v'=>5),
Array ('id'=>3,'prize'=>' speaker device ','v'=>10),
Array ('id'=>4,'prize'=>'4G usb drive ','v'=>12),
Array ('id'=>5,'prize'=>'10Q COINS ','v'=>22),
'5' = > array (' id '= > 6,' prize '= >' in the next time maybe you can oh ', 'v' = > 50).
);
/ *
* each request to the front page, the PHP loop awards set array,
* get the prize id of the draw through the probability calculation function get_rand.
* save the winning prize in the array $res['yes'],
* the rest of the unsuccessful winners are saved in $res['no'],
* finally, output json data to the front page.
* /
Foreach ($prize_arr as $key => $val) {
$arr [$val [' id ']] = $val [' v '];
}
$rids = get_rand ($arr); // get the award id according to the probability
$res [' yes'] = $prize_arr [$rids - 1] [' prize ']. / / in the awards
The unset ($prize_arr [$rids - 1)); // remove the winning entries from the array, leaving the missing entries
The shuffle ($prize_arr); // shuffle the array
For ($I = 0; $I < count ($prize_arr); ${i++)
$[] pr = $prize_arr [$I] [' prize '];
}
$res [' no '] = $pr;
Print_r ($res);
? >
-- -- -- -- -- -- -- --
Copyright notice: this is an original article BY CSDN blogger "cyber swordsman". CC 4.0 by-sa copyright agreement is followed.
The original link: https://blog.csdn.net/dongsg11200/article/details/24607877
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.