publicstatic List<Integer> generateRedPacket(int m, int n, int x, int y) { intminTotal= n * x; intmaxTotal= n * y; if (m > maxTotal || m < minTotal) { returnnull; }
intremaining= m - minTotal; Randomr=newRandom(); List<Integer> redPacket = newArrayList<>(n); for (inti=0; i < n; i++) { redPacket.add(x); }
publicstatic List<Integer> generateRedPacket2(int m, int n, int x, int y) { intminTotal= n * x; intmaxTotal= n * y; if (m > maxTotal || m < minTotal) { returnnull; }
intremaining= m; Randomr=newRandom(); List<Integer> redPacket = newArrayList<>(); for (inti=0; i < n; i++) { intmin= Math.max(x, remaining - (n - i - 1) * y); intmax= Math.min(y, remaining - (n - i - 1) * x);
intamount= min + r.nextInt(max - min + 1); redPacket.add(amount); remaining -= amount; }