# 看完微信抢红包算法你就明白，为啥你不是手气最佳

2022/03/29 11:26

## 算法一：剩余金额随机法

// 分配红包的算法
private static void testPocket(BigDecimal amount, BigDecimal min, BigDecimal num) {
BigDecimal remain = amount.subtract(min.multiply(num));
final Random random = new Random();
final BigDecimal hundred = new BigDecimal("100");
BigDecimal sum = BigDecimal.ZERO;
BigDecimal redpeck ;
for (int i = 0; i < num.intValue(); i++) {
final int nextInt = random.nextInt(100);
if (i == num.intValue() - 1) {
redpeck = remain;
} else {
redpeck = new BigDecimal(nextInt).multiply(remain).divide(hundred, 2, RoundingMode.FLOOR);
}
if (remain.compareTo(redpeck) > 0) {
remain = remain.subtract(redpeck);
} else {
remain = BigDecimal.ZERO;
}
System.out.println("第" + (i + 1) + "个人抢到红包金额为：" + min.add(redpeck).setScale(2, BigDecimal.ROUND_HALF_UP));
}
System.out.println("红包总额：" + sum.setScale(2, BigDecimal.ROUND_HALF_UP));
}
// 测试代码
public static void main(String[] args) {
BigDecimal amount = new BigDecimal(100).setScale(2, BigDecimal.ROUND_HALF_UP);
BigDecimal min = new BigDecimal(0.01).setScale(2, BigDecimal.ROUND_HALF_UP);
BigDecimal num = new BigDecimal(10).setScale(2, BigDecimal.ROUND_HALF_UP);
testPocket2(amount,min,num);
}

## 算法二：整体随机法

private static void testPocket2(BigDecimal amount,BigDecimal min ,BigDecimal num){
final Random random = new Random();
final int[] rand = new int[num.intValue()];
BigDecimal sum1 = BigDecimal.ZERO;
BigDecimal redpeck ;
int sum = 0;
for (int i = 0; i < num.intValue(); i++) {
rand[i] = random.nextInt(100);
sum += rand[i];
}
final BigDecimal bigDecimal = new BigDecimal(sum);
BigDecimal remain = amount.subtract(min.multiply(num));
for (int i = 0; i < rand.length; i++) {
if(i == num.intValue() -1){
redpeck = remain;
}else{
redpeck = remain.multiply(new BigDecimal(rand[i])).divide(bigDecimal,2,RoundingMode.FLOOR);
}
if(remain.compareTo(redpeck) > 0){
remain = remain.subtract(redpeck);
}else{
remain = BigDecimal.ZERO;
}
}

System.out.println("红包总额："+sum1);
}

// 测试代码
public static void main(String[] args) {
BigDecimal amount = new BigDecimal(100).setScale(2, BigDecimal.ROUND_HALF_UP);
BigDecimal min = new BigDecimal(0.01).setScale(2, BigDecimal.ROUND_HALF_UP);
BigDecimal num = new BigDecimal(10).setScale(2, BigDecimal.ROUND_HALF_UP);
testPocket2(amount,min,num);
}

## 算法三：割线法

private static void testPocket3(BigDecimal amount, BigDecimal min, BigDecimal num) {
final Random random = new Random();
final int[] rand = new int[num.intValue()];
BigDecimal sum1 = BigDecimal.ZERO;
BigDecimal redpeck;
int sum = 0;
for (int i = 0; i < num.intValue(); i++) {
rand[i] = random.nextInt(100);
sum += rand[i];
}
final BigDecimal bigDecimal = new BigDecimal(sum);
BigDecimal remain = amount.subtract(min.multiply(num));
for (int i = 0; i < rand.length; i++) {
if (i == num.intValue() - 1) {
redpeck = remain;
} else {
redpeck = remain.multiply(new BigDecimal(rand[i]))
.divide(bigDecimal, 2, RoundingMode.FLOOR);
}
if (remain.compareTo(redpeck) > 0) {
remain = remain.subtract(redpeck).setScale(2, BigDecimal.ROUND_HALF_UP);
} else {
remain = BigDecimal.ZERO;
}
System.out.println("第" + (i + 1) + "个人抢到红包金额为：" + min.add(redpeck));
}

System.out.println("红包总额：" + sum1);
}
// 测试代码
public static void main(String[] args) {
BigDecimal amount = new BigDecimal(100).setScale(2, BigDecimal.ROUND_HALF_UP);
BigDecimal min = new BigDecimal(0.01).setScale(2, BigDecimal.ROUND_HALF_UP);
BigDecimal num = new BigDecimal(10).setScale(2, BigDecimal.ROUND_HALF_UP);
testPocket2(amount,min,num);
}

## 算法四：二倍均值法

  BigDecimal remain = amount.subtract(min.multiply(num));
final Random random = new Random();
final BigDecimal hundred = new BigDecimal("100");
final BigDecimal two = new BigDecimal("2");
BigDecimal sum = BigDecimal.ZERO;
BigDecimal redpeck;
for (int i = 0; i < num.intValue(); i++) {
final int nextInt = random.nextInt(100);
if(i == num.intValue() -1){
redpeck = remain;
}else{
redpeck = new BigDecimal(nextInt).multiply(remain.multiply(two).divide(num.subtract(new BigDecimal(i)),2,RoundingMode.CEILING)).divide(hundred,2, RoundingMode.FLOOR);
}
if(remain.compareTo(redpeck) > 0){
remain = remain.subtract(redpeck).setScale(2, BigDecimal.ROUND_HALF_UP);
}else{
remain = BigDecimal.ZERO;
}
}
System.out.println("红包总额：" + sum);
}

1 评论
10 收藏
2