Home >Backend Development >PHP Tutorial >PHP大转盘中奖概率算法实例_PHP

PHP大转盘中奖概率算法实例_PHP

WBOY
WBOYOriginal
2016-05-31 19:29:07961browse

本文实例讲述了PHP大转盘中奖概率算法的实现方法,分享给大家供大家参考。具体如下:

大转盘是最近很多线上网动中一个比较有意思的东西了,下面我们就来看看这个大转盘中奖概率算法与例子,希望对各位有所帮助。

这是一个APP客户端有大转盘抽奖算法,具体如何抽奖当然在我们服务端实现了。下面和大家简单分享一下实现代码:

代码如下:

header("Content-type: text/html; charset=utf-8");
$prize_arr = array(
'0' => array('id'=>1,'prize'=>'平板电脑','v'=>1),
'1' => array('id'=>2,'prize'=>'数码相机','v'=>5),
'2' => array('id'=>3,'prize'=>'音箱设备','v'=>10),
'3' => array('id'=>4,'prize'=>'4G优盘','v'=>12),
'4' => array('id'=>5,'prize'=>'10Q币','v'=>22),
'5' => array('id'=>6,'prize'=>'下次没准就能中哦','v'=>50),
);
 
$actor = 100;
 
foreach ($prize_arr as $v) {
$arr[$v['id']] = $v['v'];
}
foreach ($arr as &$v) {
$v = $v*$actor;
}
asort($arr);
$sum = array_sum($arr);   //总概率
 
$rand = mt_rand(1,$sum);
 
$result = '';    //中奖产品id
 
foreach ($arr as $k => $x)
{
if($rand {
$result = $k;
break;
}
else
{
$rand -= $x;
}
}
$res['yes'] = $prize_arr[$result-1]['prize']; //中奖项
print_r($res);

希望本文所述对大家的PHP程序设计有所帮助。

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn