微信红包算法

时间:2014-10-11 16:23:46   收藏:0   阅读:432
$number = 10; // 红包数
$total = 100; // 总金额
    
$wallet = array(); // 红包列表
    
// 算法
for($i = $number; $i > 0; $i--){
    $x = $i == 1 ? $total : mt_rand(1, $total/$i);
    $total -= $x;
    $wallet[] = $x;
}
shuffle($wallet);
    
// 结果
echo ‘total:‘, array_sum($wallet), ‘.‘;
echo ‘wallet:‘, join(‘,‘, $wallet);

原文:http://www.cnblogs.com/SoulJaGod/p/4019096.html

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!