当前位置 : 主页 > 网络编程 > PHP >

php抽奖概率|php概率实现|ProbobAward

来源:互联网 收集:自由互联 发布时间:2021-07-03
$award = array( 1 = 1, 2 = 3, 3 = 6, 4 = 9, 5 = 12, 6 = 22, 7 = 30, 8 = 50, 9 = 60, 10 = 99 ); echo getProbobAward($award); ?phpdefined('ROOT') or exit('Access Denied'); class Probob{ public function getProbobAward($array){ $Sum = array_sum
$award = array(
1 => 1, 
2 => 3, 
3 => 6, 
4 => 9, 
5 => 12, 
6 => 22, 
7 => 30, 
8 => 50, 
9 => 60, 
10 => 99
);
echo getProbobAward($award);
 
<?php
defined('ROOT') or exit('Access Denied');
  
class Probob{
      
    public function getProbobAward($array){
        $Sum = array_sum($array);
        foreach($array as $key => $prob){
            $Rand = mt_rand(1, $Sum);
            if($Rand <= $prob){
                return $key;
            }else{
                $Sum -= $prob;
            }
        }
    }
          
}

网友评论