PHP概率抽奖
2018-11-10 本文已影响0人
我_记忆中的自己
说明
大多数活动或游戏都会有转盘等抽奖活动,以下方法实现简单的生成转盘奖品
逻辑代码
//最后确认相加等于100
$prize = array(
1 => 3.5, //3.5%机率
2 => 2.5,
3 => 6,
4 => 2,
5 => 42, //42%机率
6 => 36,
7 => 2,
8 => 4,
9 => 2
);
// 奖品
$prizeList = array(
1 => array('长江七号'),
2 => array('3Q币'),
3 => array('10000游戏币'),
4 => array('99999游戏币'),
5 => array('谢谢参与!'),
6 => array('欢迎下次再来!'),
7 => array('10Q币'),
8 => array('洗衣液'),
9 => array('纸巾一条')
);
// unset($prize[7]); //直接把10Q这个奖品去掉
$times = 10;
$max = 0;
foreach ($prize as $k => $v) {
$max = $v * $times + $max;
$row['v'] = $max;
$row['k'] = $k;
$prizeZone[] = $row;
}
$max--; //临界值
$rand = mt_rand(0, $max);
$zone = 1;
foreach ($prizeZone as $k => $v) {
if ($rand >= $v['v']) {
if ($rand >= $prizeZone[$k + 1]['v']) {
continue;
} else {
$zone = $prizeZone[$k + 1]['k'];
break;
}
}
$zone = $v['k'];
break;
}
print_r($prizeList[$zone][0])
结果:谢谢参与!