function ratioDis($count, $data, $retKey = 'id', $ratioKey = 'ratio', $disKey = 'dis')
{
//计算对应总数
$start = 0;
$dataCount = count($data);
$dised = 0;
for ($i = 0; $i < $dataCount; $i++) {
$isLast = ($i == $dataCount - 1) ? true : false;
$max = $isLast ? $count - $dised : intval($count * $data[$i][$ratioKey]);
$dised += $max;
//已排满的去除
if ($data[$i][$disKey] >= $max) {
unset($data[$i]);
$start += $max;
} else {
$data[$i]['_max_'] = $max;
}
}
//算法分配
$prevMax = $start;
$rand = mt_rand($start + 1, $count);
foreach ($data as $val) {
if ($rand > $prevMax && $rand <= $val['_max_'] + $prevMax) {
return $val[$retKey];
}
$prevMax
按比例分配算法
最新推荐文章于 2021-07-11 23:04:08 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)