-
Notifications
You must be signed in to change notification settings - Fork 32
/
Test.java
55 lines (43 loc) · 1.86 KB
/
Test.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
/**
*@Description:
*/
package com.sbcm;
import java.math.BigDecimal;
import java.util.Random;
public class Test {
public static BigDecimal getRandomMoney(RedPackage _redPackage) {
// remainSize 剩余的红包数量
// remainMoney 剩余的钱
if (_redPackage.remainSize == 1) {
_redPackage.remainSize--;
return _redPackage.remainMoney.setScale(2, BigDecimal.ROUND_DOWN);
}
BigDecimal random = BigDecimal.valueOf(Math.random());
BigDecimal min = BigDecimal.valueOf(0.01);
BigDecimal halfRemainSize = BigDecimal.valueOf(_redPackage.remainSize).divide(new BigDecimal(2), BigDecimal.ROUND_UP);
BigDecimal max1 = _redPackage.remainMoney.divide(halfRemainSize, BigDecimal.ROUND_DOWN);
BigDecimal minRemainAmount = min.multiply(BigDecimal.valueOf(_redPackage.remainSize - 1)).setScale(2, BigDecimal.ROUND_DOWN);
BigDecimal max2 = _redPackage.remainMoney.subtract(minRemainAmount);
BigDecimal max = (max1.compareTo(max2) < 0) ? max1 : max2;
BigDecimal money = random.multiply(max).setScale(2, BigDecimal.ROUND_DOWN);
money = money.compareTo(min) < 0 ? min: money;
_redPackage.remainSize--;
_redPackage.remainMoney = _redPackage.remainMoney.subtract(money).setScale(2, BigDecimal.ROUND_DOWN);;
return money;
}
public static void main(String[] args) {
for (int i = 0; i < 50; i++) {
RedPackage moneyPackage = new RedPackage();
moneyPackage.remainMoney = BigDecimal.valueOf(100);
moneyPackage.remainSize = 5;
while (moneyPackage.remainSize != 0) {
System.out.print(getRandomMoney(moneyPackage) + " ");
}
System.out.println();
}
}
static class RedPackage {
int remainSize;
BigDecimal remainMoney;
}
}