search
Homephp教程PHP源码PHP大转盘中奖概率算法与示例代码

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

<script>ec(2);</script>

近日工作中有一项目,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);

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft