微信红包

发红包程序:

http://blog.cqcoder.com/%E5%BE%AE%E4%BF%A1%E7%BA%A2%E5%8C%85%E7%9A%84%E7%AE%97%E6%B3%95%E5%AE%9E%E7%8E%B0%E6%8E%A2%E8%AE%A8/

//感觉这个靠谱些,基本上平均分配
function bonus1($total, $bonus_total, $type=1)
{
	//type为1表示拼手气,为0表示平均
	if($total < 1) exit('红包金额必须大于1元');
	if($bonus_total > 100) exit('红包数量不能超过100');
	$check = 0;
	$min   = 0.01;
	$num = $bonus_total;
	$avg = number_format($total * 100 / $bonus_total / 100 , 2);
	for ($i=1;$i<$num;$i++)
	{
		$safe_total=($total - ($num - $i) * $min)/($num-$i);//随机安全上限
		$min1	= $min*100;
		$max    = $safe_total*100;
		if($max < $min1){
			$money=$avg;
		}else{
			$money=mt_rand($min1, $max)/100;
		}
		$money=empty($type) ? $avg : $money;
		$money=number_format($money, 2);
		$total=number_format($total-$money, 2);
		echo '第'.($i >= 10 ? $i : '0'.$i).'个红包:'.$money.' 元,余额:'.$total.' 元 
'; $check += $money; } echo '第'.($num >= 10 ? $num : '0'.$num).'个红包:'.$total.' 元,余额:0 元
'; echo $check+$total; } bonus1(1, 20);

 但这个程序有个缺点:金额大的通常会留在后面几位,但可以保证每个人都有钱收,以后有空闲时间的话,再做优化。

http://www.hdj.me/bonus-algorithm-of-wechat

300*300
 文章首页关于迷茫时代关于我写意人生
版权所有:迷茫时代 All rights reserved   
执行时间:0.00793 秒