抽奖金额问题

This commit is contained in:
wangw 2025-01-15 10:26:20 +08:00
parent d85348c2b1
commit 6e8f527643
1 changed files with 24 additions and 6 deletions

View File

@ -48,6 +48,8 @@ public class DiscSpinningServiceImpl extends ServiceImpl<DiscSpinningDao, DiscSp
private final WuyouPay wuyouPay; private final WuyouPay wuyouPay;
private final PlatformTransactionManager transactionManager; private final PlatformTransactionManager transactionManager;
private Random random = new Random();
@Autowired @Autowired
public DiscSpinningServiceImpl(CommonInfoService commonRepository, public DiscSpinningServiceImpl(CommonInfoService commonRepository,
@ -116,8 +118,8 @@ public class DiscSpinningServiceImpl extends ServiceImpl<DiscSpinningDao, DiscSp
recordService.save(record); recordService.save(record);
return record; return record;
} }
Random random = new Random();
BigDecimal randomNum = new BigDecimal(random.nextInt(maxNumber.intValue())); BigDecimal randomNum = new BigDecimal(getRandomInt(maxNumber.intValue()));
List<DiscSpinningAmount> amounts = new ArrayList<>(); List<DiscSpinningAmount> amounts = new ArrayList<>();
Map<String, List<DiscSpinningAmount>> amountMaps = redisUtils.getMapData(RedisKeys.getDateKey("spinning:amount:") + source, "setDiscSpinningAmounts", DiscSpinningAmount.class); Map<String, List<DiscSpinningAmount>> amountMaps = redisUtils.getMapData(RedisKeys.getDateKey("spinning:amount:") + source, "setDiscSpinningAmounts", DiscSpinningAmount.class);
if (CollectionUtil.isNotEmpty(amountMaps)) { if (CollectionUtil.isNotEmpty(amountMaps)) {
@ -134,14 +136,12 @@ public class DiscSpinningServiceImpl extends ServiceImpl<DiscSpinningDao, DiscSp
int maxAmount = Integer.parseInt(commonRepository.findOne(900).getValue()); int maxAmount = Integer.parseInt(commonRepository.findOne(900).getValue());
double resultAmount = 0; double resultAmount = 0;
if (prize.getType() == 2) { if (prize.getType() == 2) {
double baseRandom = RandomUtil.randomDouble(0.01, 1); double baseRandom = getRandomDouble();
double baseAmount = 0;
for (DiscSpinningAmount amount : amounts) { for (DiscSpinningAmount amount : amounts) {
if (baseRandom < amount.getRandom()) { if (baseRandom < amount.getRandom()) {
resultAmount = baseAmount + RandomUtil.randomDouble(0.01, 1) * (amount.getMaxAmount() - baseAmount); resultAmount = getRandomDouble() * amount.getMaxAmount();
break; break;
} }
baseAmount = amount.getMaxAmount();
} }
if (resultAmount < 0.01) { if (resultAmount < 0.01) {
@ -193,5 +193,23 @@ public class DiscSpinningServiceImpl extends ServiceImpl<DiscSpinningDao, DiscSp
int value = random.nextInt(maxValue) * random.nextInt(maxValue); int value = random.nextInt(maxValue) * random.nextInt(maxValue);
return new BigDecimal(value / 100); return new BigDecimal(value / 100);
} }
public double getRandomDouble() {
random.setSeed(UUID.randomUUID().hashCode());
return random.nextDouble();
}
public int getRandomInt(int maxValue) {
random.setSeed(UUID.randomUUID().hashCode());
return random.nextInt(maxValue);
}
public static void main(String[] args) {
Random random = new Random();
for (int i = 0; i < 500; i++) {
random.setSeed(UUID.randomUUID().hashCode());
System.out.println(random.nextInt(100)*random.nextInt(100));
}
}
} }