import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.Arrays;
import java.util.Random;
/**
* Created by limi on 2019/1/3.
*/
public class Main {
public static void main(String args[]){
// 微信红包拆分测试
int money = 10000; //单位(分)
int num = 10;
System.out.println("总金额:"+money/100+"元\t 拆分红包总个数:" + num);
Integer[] array = divived(money,num);
DecimalFormat df = new DecimalFormat("######0.00");
for (Integer i:array){
System.out.print(df.format(new BigDecimal(i).divide(new BigDecimal(100)))+"元 ");
}
}
/****
* 微信红包拆分方法
* @param money 被拆分的总金额 (单位分)
* @param n 被拆分的红包个数
* @return 拆分后的每个红包金额数组
*/
public static Integer[] divived(int money, int n){
Random random = new Random();
Integer[] array = new Integer[n];
Integer restPeopleNum = n;
for(int i = 0;i<n;i++){
int redNum = random.nextInt(money / restPeopleNum * 2 ) + 1;
if(i==n-1){
array[i] = money;
}else{
restPeopleNum--;
array[i] = redNum;
money -= redNum;
}
}
return array;
}
}