Skip to content

Instantly share code, notes, and snippets.

@techom
Created March 8, 2016 06:43
Show Gist options
  • Save techom/7622b85c6281e78a13a0 to your computer and use it in GitHub Desktop.
Save techom/7622b85c6281e78a13a0 to your computer and use it in GitHub Desktop.
微信红包算法测试
header("Content-Type: text/html;charset=utf-8");
$total=10; // 红包总额
$num=8; // 分成8个红包,支持8人随机领取
$min=0.01; // 每个人最少能收到0.01元
for ($i=1;$i<$num;$i++)
{
$safe_total = ($total-($num-$i)*$min)/($num-$i); //随机安全上限
$money = mt_rand($min*100, $safe_total*100)/100;
$total = $total - $money;
echo '第'.$i.'个红包:'.$money.' 元,余额:'.$total.' 元 <br/>';
}
echo '第'.$num.'个红包:'.$total.' 元,余额:0 元';
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment