方法一:二倍均值法

  1. import java.math.BigDecimal;
  2. import java.util.ArrayList;
  3. import java.util.List;
  4. import java.util.Random;
  5. /**
  6. * 抢红包
  7. * </p>
  8. * <a href="https://juejin.im/post/5af80310f265da0b8636585e">参考</p>
  9. *
  10. * @author MinGRn <br > MinGRn97@gmail.com
  11. * @date 10/10/2018 09:48
  12. */
  13. public class DivideRedPackage {
  14. /**
  15. * 发红包算法,金额参数以分为单位
  16. */
  17. private static List<Integer> divideRedPackage(Integer totalAmount, Integer totalPeopleNum) {
  18. List<Integer> amountList = new ArrayList<>();
  19. Integer restAmount = totalAmount;
  20. Integer restPeopleNum = totalPeopleNum;
  21. Random random = new Random();
  22. for (int i = 0; i < totalPeopleNum - 1; i++) {
  23. /*
  24. *随机范围:[1,剩余人均金额的两倍),左闭右开
  25. */
  26. int amount = random.nextInt(restAmount / restPeopleNum * 2 - 1) + 1;
  27. restAmount -= amount;
  28. restPeopleNum--;
  29. amountList.add(amount);
  30. }
  31. amountList.add(restAmount);
  32. return amountList;
  33. }
  34. public static void main(String[] args) {
  35. List<Integer> amountList = divideRedPackage(5000, 30);
  36. for (Integer amount : amountList) {
  37. System.out.println("抢到金额:" + new BigDecimal(amount).divide(new BigDecimal(100)));
  38. }
  39. }
  40. }

参考:

方法二:

  1. import java.util.Random;
  2. /**
  3. * 抢红包
  4. * </p>
  5. * <a href="https://blog.csdn.net/lb_383691051/article/details/79379384">参考</p>
  6. *
  7. * @author MinGRn <br > MinGRn97@gmail.com
  8. * @date 10/10/2018 12:17
  9. */
  10. public class DivideRedPackage {
  11. public static void main(String[] args) {
  12. Integer count = 10;
  13. Boolean isHave = true;
  14. MoneyPackage moneyPackage = new MoneyPackage((double) 100, 10);
  15. while (isHave) {
  16. if ((--count) <= 0) {
  17. isHave = false;
  18. }
  19. System.out.println(divideRedPackage(moneyPackage));
  20. }
  21. }
  22. /**
  23. * 随机获取红包
  24. *
  25. * @param moneyPackage 红包
  26. */
  27. private static double divideRedPackage(MoneyPackage moneyPackage) {
  28. if (moneyPackage.peopleNum == 1) {
  29. moneyPackage.peopleNum--;
  30. return (double) Math.round(moneyPackage.amount * 100) / 100;
  31. }
  32. double min = 0.01, max = moneyPackage.amount / moneyPackage.peopleNum * 2;
  33. double money = new Random().nextDouble() * max;
  34. money = money <= min ? min : money;
  35. money = Math.floor(money * 100) / 100;
  36. moneyPackage.peopleNum--;
  37. moneyPackage.amount -= money;
  38. return money;
  39. }
  40. static class MoneyPackage {
  41. /**
  42. * 红包总额
  43. */
  44. Double amount;
  45. /**
  46. * 红包数
  47. */
  48. Integer peopleNum;
  49. public Double getAmount() {
  50. return amount;
  51. }
  52. public void setAmount(Double amount) {
  53. this.amount = amount;
  54. }
  55. public Integer getPeopleNum() {
  56. return peopleNum;
  57. }
  58. public void setPeopleNum(Integer peopleNum) {
  59. this.peopleNum = peopleNum;
  60. }
  61. MoneyPackage(Double amount, Integer peopleNum) {
  62. this.amount = amount;
  63. this.peopleNum = peopleNum;
  64. }
  65. }
  66. }

参考: