`

简单的概率问题 设置

    博客分类:
  • PHP
阅读更多
        //修改此处请修改getPrize 中默认数据
        $con = [
            ['typeId' => 1, 'price' => 50],
            ['typeId' => 1, 'price' => 120],
            ['typeId' => 4, 'price' => 1],
            ['typeId' => 4, 'price' => 2],
            ['typeId' => 4, 'price' => 5],
            ['typeId' => 2, 'price' => 199],
        ];

        $lotteryChance = [100, 0, 0, 0, 0, 100];
        $total = array_sum($lotteryChance) / (1 - 0.2);
        $rand = rand(0, $total - 1);
        $start = 0;
        for ($i = 0; $i < count($lotteryChance); $i++) {
            $end = $start + $lotteryChance[$i];
            try{
                if ($rand >= $start && $rand < $end) {
                    return $con[$i];
                }
            }catch(\Exception $e){
                return null;
            }
            $start += $lotteryChance[$i];
        }

    

    网络上抽奖概率的文章

http://blog.csdn.net/leeyisoft/article/details/8226036
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics