首页 > 代码库 > 抽奖概率计算

抽奖概率计算

function get_rand($proArr) {
        $result = ‘‘;
        //概率数组的总概率精度
        $proSum = array_sum($proArr);
        //概率数组循环
        foreach ($proArr as $key => $proCur) {
            $randNum = mt_rand(1, $proSum);
            if ($randNum <= $proCur) {
                $result = $key;
                break;
            } else {
                $proSum -= $proCur;
            }
        }
        unset ($proArr);
        return $result;
    }
$rid = $this->get_rand($arr); //根据概率获取奖项id

foreach($plist as $key => $val){ if($val[‘prize_id‘] == $rid){ $res[‘yes‘] = $val[‘prize_name‘]; //中奖项 } }

  

抽奖概率计算