微信红包算法

时间:2014-10-11 16:42:35   收藏:0   阅读:356
$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);
评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!