微信红包算法

$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

以上是微信红包算法的全部内容。
THE END
分享
二维码
< <上一篇
下一篇>>