随机红包算法就是给定一个具体的红包金额,具体的红包个数,还有给定红包的最大金额和最小金额,按照业务可以分成两种,一种是预先生成,一种是实时生成
1、预先生成
public function getBonus($money, $num, $min, $max) //$num是总的红包数量
{
$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;
}
return $data;
}
2、实时生成
public function getBonus($money, $num, $min, $max) //$num是剩余红包数量
{
$num-=1;
if ($num * $min >= $money) {
throw new \Exception('最小金额超出范围');
}
if ($num * $max <= $money) {
throw new \Exception('最大金额太小');
}
$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);
return $k;
}