PHP抽奖程序概率算法
//概率算法,6个奖项 $prize_arr = array( '0' => array('id'=>1,'prize'=>'iphone6','v'=>1), '1' => array('id'=>2,'prize'=>'数码相机','v'=>5), '2' => array('id'=>3,'prize'=>'音箱设备','v'=>10), '3' => array('id'=>4,'prize'=>'50Q币','v'=>24), '4' => array('id'=>5,'prize'=>'10Q币','v'=>60), '5' => array('id'=>6,'prize'=>'1Q币','v'=>1900), ); //每个奖品的中奖几率,奖品ID作为数组下标 foreach($prize_arr as $val){ $item[$val['id']] = $val['v']; } function get($item){ //中奖概率基数 $num = array_sum($item);//当前一等奖概率1/2000 foreach($item as $k=>$v){ //获取一个1到当前基数范围的随机数 $rand = mt_rand(1,$num); if($rand <= $v){ //假设当前奖项$k=2,$v1900,则没中六等奖,总获奖基数2000-1900,前五次循环都没中则2000-1-5-10-24-60=1900,必中6等奖,哈哈 $num -= $v; } } return $res; } $res = get($item); $h = $prize_arr[$res-1]['prize']; echo $h.'; ';
Copy after login
Statement of this Website
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
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
What's New in Windows 11 KB5054979 & How to Fix Update Issues
3 weeks ago
By DDD
How to fix KB5055523 fails to install in Windows 11?
2 weeks ago
By DDD
InZoi: How To Apply To School And University
3 weeks ago
By DDD
How to fix KB5055518 fails to install in Windows 10?
2 weeks ago
By DDD
Roblox: Dead Rails – How To Summon And Defeat Nikola Tesla
4 weeks ago
By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

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