Home php教程 PHP源码 PHP抽奖程序概率算法

PHP抽奖程序概率算法

May 23, 2016 pm 04:36 PM

跳至

   //概率算法,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][&#39;prize&#39;];
echo $h.&#39;; &#39;;
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

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

Video Face Swap

Video Face Swap

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

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)