这篇文章主要介绍了PHP大转盘中奖概率算法,实例讲述了概率算法的实现过程,非常具有实用价值,需要的朋友可以参考下
本文实例讲述了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程序设计有所帮助。
,
Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Dreamweaver Mac version
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

WebStorm Mac version
Useful JavaScript development tools