# 微信随机生成红包金额算法java版

```public class WxAlgorithm{

/**
* @param moneySum 输入总金额
* @param redNum 输入红包数量
*/
private static void wxAlgorithm(double moneySum, int redNum) {
// 设置最小的金额
double moneyMin = 0.01;
Random random = new Random();
//精确小数点2位
NumberFormat formatter = new DecimalFormat("#.##");
for (int i=1;i<redNum;i++)
{
//随机一个数，数值范围在最小值与余额之间
String money = formatter.format(random.nextDouble() * (moneySum - moneyMin) + moneyMin);
//数值转换
moneySum = Double.valueOf(formatter.format(moneySum - Double.valueOf(money)));

System.out.println("第"+i+"个红包：" + money + "元 ,余额：" + moneySum);
}

System.out.println("最后个红包：" + moneySum + "元 ,余额：" + (moneySum - moneySum));
}

public static void main(String[] args) {
wxAlgorithm(10.26, 9);
}
}
```

```public static double [] getMoney(double money, int num){
Random r = new Random();
DecimalFormat format = new DecimalFormat(".##");

double middle = Double.parseDouble(format.format(money/num));
double [] dou = new double[num];
double redMoney = 0;
double nextMoney = money;
double sum = 0;
int index = 0;
for(int i=num;i>0;i--){
if(i == 1){
dou[index] = nextMoney;
}else{
while(true){
String str = format.format(r.nextDouble()*nextMoney);
redMoney = Double.parseDouble(str);
if(redMoney>0 && redMoney < middle){
break;
}
}
nextMoney = Double.parseDouble(format.format(nextMoney - redMoney));
sum = sum + redMoney;
dou[index] = redMoney;
middle = Double.parseDouble(format.format(nextMoney/(i-1)));
index++;
}
}
return dou;
}
```

```package com.sunron.test;

import java.math.BigDecimal;

import org.junit.Test;

public class HongBao {
@Test
public void testHonbao(){
hb(100, 9, 0.01);//金额，个数，最少值
//zb();
}
void hb(double total,int num,double min){
for(int i=1;i<num;i++){
double safe_total=(total-(num-i)*min)/(num-i);
double money=Math.random()*(safe_total-min)+min;
BigDecimal money_bd=new BigDecimal(money);
money=money_bd.setScale(2,BigDecimal.ROUND_HALF_UP).doubleValue();
total=total-money;
BigDecimal total_bd=new BigDecimal(total);
total=total_bd.setScale(2,BigDecimal.ROUND_HALF_UP).doubleValue();
System.out.println("第"+i+"个红包："+money+",余额为:"+total+"元");
}
System.out.println("第"+num+"个红包："+total+",余额为:0元");
}

void zb(){
for(int a=0;a<=10000;a++){
if(a % 1000== 0)
System.out.println (a);
}

}

}

```

```public static void main(String[] args) {
//红包数
int number = 10;
//红包总额
float total = 100;
float money;
//最小红包
double min = 1;
double max;
int i = 1;
List math = new ArrayList();
DecimalFormat df = new DecimalFormat("###.##");
while (i < number) {
//保证即使一个红包是最大的了,后面剩下的红包,每个红包也不会小于最小值
max = total - min * (number - i);
int k = (int)(number - i) / 2;
//保证最后两个人拿的红包不超出剩余红包
if (number - i <= 2) {
k = number - i;
}
//最大的红包限定的平均线上下
max = max / k;
//保证每个红包大于最小值,又不会大于最大值
money = (int) (min * 100 + Math.random() * (max * 100 - min * 100 + 1));
money = (float)money / 100;
//保留两位小数
money = Float.parseFloat(df.format(money));
total=(int)(total*100 - money*100);
total = total/100;
System.out.println("第" + i + "个人拿到" + money + "剩下" + total);
i++;
//最后一个人拿走剩下的红包
if (i == number) {