红包算法

public function RandomMoney($money, $num)
    {
        $arr = array();
        $total_money = 0;
        $this_money = $money;
        $total_num = $num;

        for ($i = 0; $i < $num; $i++) {
            if ($total_num == 1) {
                $m = $this_money;
            } else {
                $m = round(rand(1, $this_money / $total_num * 1.5));
                //相差不大的分法
                //$m = round(rand($this_money / $total_num + 20, $this_money / $total_num - 20));
            }
            $arr[] = $m;
            $total_money += $m;
            $this_money -= $m;
            $total_num--;
        }
        echo "总金额:$total_money<br>";
        dd($arr);
    }
posted @ 2022-03-29 15:43  心之所依  阅读(44)  评论(0编辑  收藏  举报