php按中奖比例抽奖

版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/qq_33980878/article/details/82701563
    public function selectTitle()
    {
        //定义奖品1,2,3的中奖概率
        $pro = [
            '1' =>16,
            '2' =>29,
            '3' =>55,
        ];
        $ret = '';
        $sum = array_sum($pro);
        foreach($pro as $k => $v) {
            $r = mt_rand(1, $sum);
            if($r <= $v){
                $ret = $k;
                break;
            } else {
                $sum = max(0, $sum - $v);
            }
        }
        if ($ret == 1) {
            $res = "奖品1";
        } elseif ($ret ==2) {
            $res = "奖品2";
        } elseif ($ret ==3) {
            $res = "奖品3";
        }
        return $res;
    }

 

展开阅读全文

没有更多推荐了,返回首页