一个抽奖小程序,概论可控,也可某个奖品在前端显示,而程序中根本不可能获得,把所有的概率x10后相加起来,新数组中每项的值等于它前几个的和加上它本身,然后随即生成一个0到最大数之间的一个数,通过循环查看它属于那个区间,返回该区间的key,代码如下:
- $prize = array(
- 1 => 3.5,
- 2 => 2.5,
- 3 => 6,
- 4 => 2,
- 5 => 42,
- 6 => 36,
- 7 => 2,
- 8 => 4,
- 9 => 2
- );
-
- $prizeList = array(
- 1 => array('小仙子'),
- 2 => array('5Q币'),
- 3 => array('38888游戏币'),
- 4 => array('88888游戏币'),
- 5 => array('谢谢参与!'),
- 6 => array('欢迎下次再来!'),
- 7 => array('10Q币'),
- 8 => array('888银元宝(绑定)'),
- 9 => array('随机高级坐骑(绑定)')
- );
-
-
- $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]);
|