算分抽奖v1

算分抽奖v1

算分抽奖v1

什么是算分抽检,算分抽奖的意思就是调整概率性的一个抽奖方式,类似与如果这个奖品价值比较高那他的算分概率比较低,但是其他的附属品可以算分比较高一点

业务代码

@Test
public void show() {
    //起始算分
    int score = 1;
    List<String> list = new ArrayList<String>();
    list.add("鱼香肉丝");
    list.add("鱼香煎蛋");
    list.add("香河肉饼");
    Map<String, Object> map = new HashMap<String, Object>();
    for (String integer : list) {
        if (integer.equals("鱼香肉丝")) {
            map.put(integer, 3);
        } else {
            map.put(integer, score);
        }
    }
    List<String> cj = new ArrayList<String>();
    for (String integer : map.keySet()) {
        Integer scre = (Integer) map.get(integer);
        for (int i = 0; i < scre; i++) {
            cj.add(integer);
        }
        log.info("奖品:{},概率:0.{}", integer, scre);
    }
    int yxrs = 0;
    int yxjd = 0;
    int xhrb = 0;

    //抽奖100次
    for (int i = 0; i < 100; i++) {
        Collections.shuffle(cj);
        int randomInt = RandomUtil.randomInt(0, cj.size() - 1);
        if (cj.get(randomInt).equals("鱼香肉丝")) {
            yxrs++;
        } else if (cj.get(randomInt).equals("香河肉饼")) {
            xhrb++;
        } else if (cj.get(randomInt).equals("鱼香煎蛋")) {
            yxjd++;
        }
    }
    log.info("中奖鱼香肉丝次数:{}", yxrs);
    log.info("中奖鱼香煎蛋次数:{}", yxjd);
    log.info("中奖香河肉饼次数:{}", xhrb);
}

概率图:

image-20220805151036004

弊端:

  • 多次循环次数嵌套问题,建议加入Math函数四舍五入的方式避免业务影响问题,满百分次数100,如果概率十分的低的话建议更换算法
THE END
喜欢就支持一下吧
点赞9 分享
评论 抢沙发
头像
欢迎您留下宝贵的见解!
提交
头像

昵称

取消
昵称表情代码图片

    暂无评论内容