中奖概率算法java
时间: 2023-11-20 18:57:56 浏览: 175
以下是两种Java中奖概率算法的例子:
1. 根据奖品过期日期计算中奖概率
```java
public double calculateProbability(Date expireDate, Date currentDate, int decayType) {
double probability = 0.0;
long expireTime = expireDate.getTime();
long currentTime = currentDate.getTime();
long timeDiff = expireTime - currentTime;
if (timeDiff > 0) {
switch (decayType) {
case 1:
probability = (double) timeDiff / expireTime;
break;
case 2:
probability = Math.pow((double) timeDiff / expireTime, 2);
break;
// 可以添加更多的衰减类型
default:
probability = (double) timeDiff / expireTime; break;
}
}
return probability;
}
```
2. 逢“几”中奖算法
```java
public int calculateLuckyNumber(int totalPeople, int totalPrizes, int N) {
int luckyNumber = (int) Math.ceil((double) totalPeople / totalPrizes) * N; return luckyNumber;
}
```
阅读全文