function rateConfig()
{
return [
1 => 0.397,
2 => 0.400,
3 => 0.203,
];
}

function randRate()
{
$rateConfig = rateConfig();
foreach ($rateConfig as $k => $v) {
$rateConfig[$k] = $v * 10000;
}

//概率数组的总概率精度
$proSum = array_sum($rateConfig);

//概率数组循环
foreach ($rateConfig as $key => $proCur) {
$randNum = mt_rand(1, $proSum);
if ($randNum <= $proCur) {
$result = $key;
break;
} else {
$proSum -= $proCur;
}
}
unset ($proArr);
return $result;
}

Comments are closed.

Post Navigation