由于在面试中多次被问到过这个算法,特地学习做笔记 。抽奖概率算法流程是通过计算概率的和,再生成处于这个范围的随机数。循环判断随机数所处的哪个范围,如果找到就返回当前范围的key,如果没找到就减去当前范围,继续循环找。最后通过key找到对应的奖项。
/**
* 生成抽奖抽中项
* @param $proArr
* @return int|string
*/
function getRand($proArr)
{
$result = '';
//概率数组的总概率精度
$proSum = array_sum($proArr);
//概率数组循环
foreach ($proArr as $key => $proCur) {
// 生成范围内随机值
$randNum = mt_rand(1, $proSum);
// 判断是否在当前范围内,如果是记录key,如果不是则在总概率中减去当前范围,并继续判断下一个
if ($randNum <= $proCur) {
$result = $key;
break;
} else {
// (缩小范围)
$proSum -= $proCur;
}
}
unset ($proArr);
// 返回key
return $result;
}
// 奖项和概率(可以保存到redis的hash中,如果被抽中把除未中奖外的概率置0)
$prizeArr = [
['id' => 1, 'prize' => '一等奖', 'v' => 1],
['id' => 2, 'prize' => '二等奖', 'v' => 5],
['id' => 3, 'prize' => '三等奖', 'v' => 10],
['id' => 4, 'prize' => '四等奖', 'v' => 12],
['id' => 5, 'prize' => '五等奖', 'v' => 22],
['id' => 6, 'prize' => '未中奖', 'v' => 50],
];
// 生成id和奖项映射
foreach ($prizeArr as $key => $val) {
$arr[$val['id']] = $val['v'];
}
// 获取抽中的key
$rid = getRand($arr);
// 获取结果
echo $prizeArr[$rid - 1]['prize'];
最新评论
Forex wiki. https://lt.forex-stock-bitcoin-brokers.com
Magnificent items from you, man. I have take note your stuff
Following on from the 3rd March Meetings held by economic de
It is remarkable, rather valuable message dfgdlfg2131.32
一般都会有一个沙盒期的,过了沙盒期就会慢慢放出来
百度不收录是应为是新站的原因吗?
The spike in consumer prices that left inflation at a four-d