php简单的抽奖算法

180it 2020-02-18 PM 2060℃ 0条
<?php
$prizes = [0 => ['jine' => 100, 'luck' => 10],
    1 => ['jine' => 80, 'luck' => 20],
    2 => ['jine' => 60, 'luck' => 40],
    3 => ['jine' => 40, 'luck' => 60],
    4 => ['jine' => 20, 'luck' => 80],
    5 => ['jine' => 10, 'luck' => 100],
    6 => ['jine' => 5, 'luck' => 120]];

$zjnumber = array_sum(array_column($prizes, 'luck'));
//获得中奖值
$zjhm = rand(1, $zjnumber);

//奖品信息
$zjxx = "";
//中奖几率
$zjjl = 0;

foreach ($prizes as $vo) {

    $zjjl += $vo['luck'];
    if ($zjjl > $zjhm) {
        $zjxx = $vo;
        break;
    }
}
echo $zjhm;
print_r('恭喜中奖' . $zjxx['jine'] . '这个奖品的中奖几率是' . $zjnumber . '分之' . $zjxx['luck']);

?>
支付宝打赏支付宝打赏 微信打赏微信打赏

如果文章或资源对您有帮助,欢迎打赏作者。一路走来,感谢有您!

标签: none

php简单的抽奖算法