1.9K Star 4.1K Fork 2.3K

JEECG / jeewx

 / 详情

LotteryUtil抽奖概率计算不均等问题

待办的
创建于  
2019-01-15 23:24

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);

评论 (0)

zhwq1216 创建了任务

登录 后才可以发表评论

状态
负责人
里程碑
Pull Requests
关联的 Pull Requests 被合并后可能会关闭此 issue
分支
开始日期   -   截止日期
-
置顶选项
优先级
参与者(1)
微信
1
https://gitee.com/jeecg/jeewx.git
git@gitee.com:jeecg/jeewx.git
jeecg
jeewx
jeewx

搜索帮助