代码拉取完成,页面将自动刷新
LotteryUtil这个类,抽中奖后,中几等奖的概率计算不正确,需要在第一次计算随机值,排除不中奖概率后重新再次计算随机值才行。
测试代码为:
public static void main(String[] args) {
List<Double> orignalRates = new ArrayList<>(6);
orignalRates.add(new Double("0.002"));
orignalRates.add(new Double("0.001"));
orignalRates.add(new Double("0.001"));
orignalRates.add(new Double("0.003"));
orignalRates.add(new Double("0.001"));
orignalRates.add(new Double("0.001"));
List<AtomicInteger> idxList = new ArrayList<>();
for(int i=0;i<orignalRates.size();i++){
idxList.add(new AtomicInteger(0));
}
int count = 100000;
for(int i=0;i<count;i++){
int idx = lottery(orignalRates);
if(idx!=-1){
System.out.println("i="+i+", idx="+idx);
idxList.get(idx).incrementAndGet();
}
}
SimpleFormat format = new SimpleFormat();
for(int i=0;i<idxList.size();i++){
System.out.println("idx="+i+",rate="+ format.number((((double)idxList.get(i).get())/(double)count),"#.#########"));
}
}
修正方法为,nextDouble使用随机函数再计算一次即可。
// 根据区块值来获取抽取到的物品索引
double nextDouble = Math.random();
//如果区块值在不中奖的概率范围内
if (nextDouble < noLuky) {
return -1;
}
//重新设置概率
nextDouble = Math.random();
sortOrignalRates.add(nextDouble);
Collections.sort(sortOrignalRates);