/** * money数值 * num分成数量 * min最小值 * max最大值 */ public function getBonus($money=300, $num=750, $min=0.3, $max=0.5) { $data = array(); if ($min * $num > $money) { return array(); } if ($max * $num < $money) { return array(); } while ($num >= 1) { $num--; $kmix = max($min, $money - $num * $max); $kmax = min($max, $money - $num * $min); $kAvg = $money / ($num + 1); //获取最大值和最小值的距离之间的最小值 $kDis = min($kAvg - $kmix, $kmax - $kAvg); //获取0到1之间的随机数与距离最小值相乘得出浮动区间,这使得浮动区间不会超出范围 $r = ((float)(rand(1, 10000) / 10000) - 0.5) * $kDis * 2; $k = round($kAvg + $r, 2); $money -= $k; $data[] = $k; } print_r($data); }