import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
public class RedEnvelope {
public static void main(String[] args) {
List<Integer> amountList = divideRedPackage(5000, 30);
amountList.forEach(amount -> System.out.println("抢到红包:" + new BigDecimal(amount).divide(new BigDecimal(100))));
}
private static List<Integer> divideRedPackage(int totalAmount, int totalPeopleNum) {
List<Integer> amountList = new ArrayList<>();
Integer restAmount = totalAmount;
Integer restPeopleNum = totalPeopleNum;
Random random = new Random();
for (int i= 0; i<totalPeopleNum- 1; i++){
int amount = random.nextInt(restAmount / restPeopleNum * 2 - 1) + 1;
restAmount -= amount;
restPeopleNum --;
amountList.add(amount);
}
amountList.add(restAmount);
return amountList;
}
}