总结下微信红包随机函数
1 固定面额 即 输入金额生成已知的固定金额
//total 红包总金额
protected function randCoupon($total=30){
$denomination = array(10,5,3,2,1);//固定面额
$arr = array();
while ($total){
if(count($denomination)>0){
$index = rand(0,count($denomination)-1);//随机抽取一个固定面额索引
$money = $denomination[$index];
if($money>$total) continue;
array_push($arr,$money);
$total = (int)$total - (int)$money;
if($total==1){
array_push($arr,$total);
break;
}
if($total<1) break;
foreach ($denomination as $k => $item){
if($total <= $item){
array_splice($denomination,$k,1);
}
}
}
}
shuffle($arr);//重新打乱数组
return $arr;
}
2 随机金额 即输入金额及红包个数 然后生成一个随机数组
//拆分数值生成若干个和等于该数值随机值
public function randNum($total=200,$num=17) {
$min=0.01;//每个人最少能收到0.01元
for ($i=1;$i<$num;$i++) {
$safe_total = ($total - ($num-$i)*$min) / ($num-$i);//随机安全上限
if($safe_total < 0.01) $safe_total = 0.01;
$money = mt_rand($min*100,$safe_total*100)/100;
$total = $total - $money;
$data[] = round($money,2);
}
$data[] = round($total,2);
shuffle($data);
return $data;
}
版权声明:本文为qq_28570871原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。