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
原创文章,作者:Maggie-Hunter,如若转载,请注明出处:https://blog.ytso.com/tech/pnotes/10302.html