抽奖概率计算

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']; //中奖项 } }

  

posted @ 2016-12-08 19:20  FinnYY  阅读(783)  评论(0编辑  收藏  举报