我是靠谱客的博主 冷静墨镜,最近开发中收集的这篇文章主要介绍Java 实现抽奖的两种方式,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

Java实现抽奖的两种方式

方式一:随机数在哪个区间内返回区间下标

方式二:随机数加入区间点集合排序返回随机数下标

代码示例:

①抽奖入参类型为BigDecimal:

package com.cfay;

import com.google.common.collect.Lists;
import org.apache.commons.collections4.CollectionUtils;

import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @Author lcb
 * @Date 2022/11/10
 **/
public class LuckDrawDemo {

    public static void main(String[] args) {
        List<BigDecimal> drawList = Lists.newArrayList();
        drawList.add(BigDecimal.valueOf(0.15));
        drawList.add(BigDecimal.valueOf(0.3));
        drawList.add(BigDecimal.valueOf(0.4));
        //抽奖测试
        List<Integer> luckDraws = Lists.newArrayList();
        for (int j = 0; j < 1000000; j++) {
            luckDraws.add(checkLuckDraw(drawList));
        }
        //按照数字进行分组排序
        Map<Integer, List<Integer>> luckDrawMap = luckDraws.stream().collect(Collectors.groupingBy(Integer::intValue));
        //遍历打印测试结果
        luckDrawMap.forEach((key, value) -> System.out.printf("第%s个出现的次数为:%d%n", key, value.size()));
    }

    /**
     * 抽奖检查
     *
     * @param drawList 抽奖列表
     * @return 抽中奖品下标
     */
    private static int checkLuckDraw(List<BigDecimal> drawList) {
        if (CollectionUtils.isEmpty(drawList)) {
            throw new RuntimeException("抽奖异常!");
        }
        if (CollectionUtils.isNotEmpty(drawList.stream().filter(Objects::isNull).collect(Collectors.toList()))) {
            throw new RuntimeException("抽奖异常!");
        }
        return luckDrawArea1(drawList);
    }

    /**
     * 抽奖1
     *
     * @param drawList 抽奖列表
     * @return 抽中奖品下标
     */
    private static int luckDrawArea1(List<BigDecimal> drawList) {
        //所有得到概率之和
        BigDecimal probability = drawList.stream().reduce(BigDecimal.ZERO, BigDecimal::add);
        //多个抽奖区间
        List<List<BigDecimal>> draws = new ArrayList<>(drawList.size());
        BigDecimal initDraw = BigDecimal.ZERO;
        //抽奖区间
        List<BigDecimal> luckDraws;
        for (BigDecimal draw : drawList) {
            luckDraws = new ArrayList<>(2);
            //区间起点
            luckDraws.add(initDraw);
            //区间终点
            initDraw = initDraw.add(draw.divide(probability, 10, BigDecimal.ROUND_HALF_UP));
            luckDraws.add(initDraw);
            draws.add(luckDraws);
        }
        BigDecimal random = BigDecimal.valueOf(Math.random());
        //过滤出抽奖区间的位置
        return draws.indexOf(draws.stream().filter(draw ->
                draw.get(0).compareTo(random) <= 0 && draw.get(1).compareTo(random) > 0).findAny().orElseGet(ArrayList::new)) + 1;
    }

    /**
     * 抽奖2
     *
     * @param drawList 抽奖列表
     * @return 抽中奖品下标
     */
    private static int luckDrawArea2(List<BigDecimal> drawList) {
        //所有得到概率之和
        BigDecimal probability = drawList.stream().reduce(BigDecimal.ZERO, BigDecimal::add);
        //多个抽奖点
        List<BigDecimal> draws = new ArrayList<>(drawList.size() + 1);
        //抽奖点
        BigDecimal initDraws = BigDecimal.ZERO;
        for (BigDecimal draw : drawList) {
            initDraws = initDraws.add(draw.divide(probability, 10, BigDecimal.ROUND_HALF_UP));
            draws.add(initDraws);
        }
        //放入随机抽奖区间,并返回抽检区间的下标
        BigDecimal random = BigDecimal.valueOf(Math.random());
        draws.add(random);
        Collections.sort(draws);
        return draws.indexOf(random) + 1;
    }
}

②、抽奖入参类型为Double

package com.cfay;

import com.google.common.collect.Lists;
import org.apache.commons.collections4.CollectionUtils;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @Author lcb
 * @Date 2022/11/10
 **/
public class LuckDrawDemo2 {

    public static void main(String[] args) {
        List<Double> drawList = Lists.newArrayList();
        drawList.add(0.15d);
        drawList.add(0.3d);
        drawList.add(0.4d);
        //抽奖测试
        List<Integer> luckDraws = Lists.newArrayList();
        for (int j = 0; j < 1000000; j++) {
            luckDraws.add(checkLuckDraw(drawList));
        }
        //按照数字进行分组排序
        Map<Integer, List<Integer>> luckDrawMap = luckDraws.stream().collect(Collectors.groupingBy(Integer::intValue));
        //遍历打印测试结果
        luckDrawMap.forEach((key, value) -> System.out.printf("第%s个出现的次数为:%d%n", key, value.size()));
    }

    /**
     * 抽奖检查
     *
     * @param drawList 抽奖列表
     * @return 抽中奖品下标
     */
    private static int checkLuckDraw(List<Double> drawList) {
        if (CollectionUtils.isEmpty(drawList)) {
            throw new RuntimeException("抽奖异常!");
        }
        if (CollectionUtils.isNotEmpty(drawList.stream().filter(Objects::isNull).collect(Collectors.toList()))) {
            throw new RuntimeException("抽奖异常!");
        }
        if (drawList.stream().reduce(Double::sum).orElse(0d) == 0d) {
            throw new RuntimeException("抽奖异常!");
        }
        return luckDrawArea2(drawList);
    }

    /**
     * 抽奖1
     *
     * @param drawList 抽奖列表
     * @return 抽中奖品下标
     */
    private static int luckDrawArea1(List<Double> drawList) {
        //所有得到概率之和
        Double probability = drawList.stream().reduce(Double::sum).orElse(0d);
        //多个抽奖区间
        List<List<Double>> draws = new ArrayList<>(drawList.size());
        double initDraw = 0d;
        //抽奖区间
        List<Double> luckDraws;
        for (Double draw : drawList) {
            luckDraws = new ArrayList<>(2);
            //区间起点
            luckDraws.add(initDraw);
            //区间终点
            luckDraws.add(initDraw += draw / probability);
            draws.add(luckDraws);
        }
        Double random = Math.random();
        //过滤出抽奖区间的位置
        return draws.indexOf(draws.stream().filter(draw ->
                draw.get(0).compareTo(random) <= 0 && draw.get(1).compareTo(random) > 0).findAny().orElseGet(ArrayList::new)) + 1;
    }

    /**
     * 抽奖2
     *
     * @param drawList 抽奖列表
     * @return 抽中奖品下标
     */
    private static int luckDrawArea2(List<Double> drawList) {
        //所有得到概率之和
        Double probability = drawList.stream().reduce(Double::sum).orElse(0d);
        //多个抽奖点
        List<Double> draws = new ArrayList<>(drawList.size() + 1);
        //抽奖点
        double initDraws = 0d;
        for (Double draw : drawList) {
            draws.add(initDraws += draw / probability);
        }
        //放入随机抽奖区间,并返回抽检区间的下标
        Double random = Math.random();
        draws.add(random);
        Collections.sort(draws);
        return draws.indexOf(random) + 1;
    }
}

最后

以上就是冷静墨镜为你收集整理的Java 实现抽奖的两种方式的全部内容,希望文章能够帮你解决Java 实现抽奖的两种方式所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(49)

评论列表共有 0 条评论

立即
投稿
返回
顶部