Java版 微信红包算法

清华大佬耗费三个月吐血整理的几百G的资源,免费分享!....>>>

 private double[] divide(double totalMoney,int num,double minMoney){
        double[] each = new double[num];
        for(int i=0;i<num-1;i++){
            //计算最大安全值,保证不会超出  。如果安全值越大,价格波动越大  .建议是平均值
            double maxSafeMoney = (totalMoney - (num-1-i)*minMoney) / (num-1-i);
            //随机算出最小值,保证不低于最小值
            each[i] = Math.random()*(maxSafeMoney-minMoney)+minMoney;
            //计算当前剩下的钱
            totalMoney = totalMoney - each[i];
        }
        each[num-1] = totalMoney;
        return each;
    }
 
    @Test
    public void test3(){
        double[] rs = divide(200,10,0.01);
        for(double each:rs){
            System.out.format("%.2f\n",each);
        }
    }

结果:
16.70
21.37
18.89
12.13
11.35
23.21
24.32
7.39
14.10
50.57