微信红包金额随机算法

//随机,金额在0.01与剩余均值*2之间
function getRandomMoney($num,$money){
    $result = array();
    while ($num){
        if($num==1){
            $randMoney = $money;
        }else {
            $min = 0.01;
            $max = $money / $num * 2;
            $randMoney = round(mt_rand() / mt_getrandmax() * $max, 2);
            $randMoney  = $randMoney < $min ? $min:$randMoney;
            $randMoney = sprintf('%0.2f',$randMoney);
            $money -= $randMoney;
        }
        $num --;
        $result[] = $randMoney;
    }
    return $result;
}

$res = getRandomMoney(15,100);

echo implode('+',$res)."\n";


结果:
在这里插入图片描述

你可能感兴趣的