php根据概率来中奖算法

代码语言:php

所属分类:算法

下面为部分代码预览,完整代码请点击下载或在bfwstudio webide中打开

<?php
function get_prize($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;
}
$prize_arr = [
    '0' => ['id' => 1, 'prize' => '平板电脑', 'v' => 1],
    '1' => ['id' => 2, 'prize' => '数码相机', 'v' => 5],
    '2' => ['id' => 3.........完整代码请登录后点击上方下载按钮下载查看

网友评论0