奖品类:

java抽奖程序代码(java抽奖系统的设计)(1)

抽奖逻辑:

java抽奖程序代码(java抽奖系统的设计)(2)

测试类:

java抽奖程序代码(java抽奖系统的设计)(3)

测试结果:

java抽奖程序代码(java抽奖系统的设计)(4)

商品类:

package org.plusgroup.modules.app.controller.vo;import io.swagger.annotations.ApiModelProperty;import lombok.Data;import java.util.ArrayList;import java.util.List;/** * @author Administrator * @desc 奖品VO * @date 2021/7/9 14:30 */@Datapublic class PrizeVo {    @ApiModelProperty("奖品ID")    private Integer id;    @ApiModelProperty("奖品名称")    private String name;    @ApiModelProperty("奖品编号")    private String code;    @ApiModelProperty("中奖概率")    private Double rate;    public PrizeVo(Integer id, String name, String code, Double rate) {        this.id = id;        this.name = name;        this.code = code;        this.rate = rate;    }    public PrizeVo() {    }    /**     * 初始化奖品     * @return List     */    public static List<PrizeVo> init(){        List<PrizeVo> list = new ArrayList<>();        list.add(new PrizeVo(10, "文档打印券", "P1", 0.2d));        list.add(new PrizeVo(20, "照片打印券", "P2", 0.5d));        list.add(new PrizeVo(30, "5元代金券", "P3", 0.1d));        list.add(new PrizeVo(40, "谢谢参与", "P4", 0.2d));        return list;    }}

测试类:

package org.plusgroup.modules.admin.service;import org.plusgroup.modules.app.controller.vo.PrizeVo;import org.springframework.util.CollectionUtils;import java.util.*;/** * @author Administrator * @desc * @date 2021/7/9 14:43 */public class IntegralLogTest {    public static void main(String [] args){        //奖品中奖概率集合        List<Double> rates = new ArrayList<>();        //将每个奖品的概率放入概率集合        List<PrizeVo> list = PrizeVo.init();        for(PrizeVo prizeVo : list){            rates.add(prizeVo.getRate());        }        System.out.println("本次中奖商品下标:" +luckDraw(rates));        // 统计每种商品中奖次数        Map<Integer, Integer> countMap = new HashMap<>();        double num = 10000; //测试次数        for (int i = 0; i < num; i++) {            int key = luckDraw(rates);            Integer value = countMap.get(key);            countMap.put(key, value == null ? 1 : value + 1);        }        for (Map.Entry<Integer, Integer> entry : countMap.entrySet()) {            System.out.println(list.get(entry.getKey()) + ", 命中次数=" + entry.getValue() + ", 实际概率=" + entry.getValue() / num);        }    }    /**     * 抽奖逻辑     * @param rates 概率集合     * @return int     */    public static int luckDraw(List<Double> rates) {        if (CollectionUtils.isEmpty(rates)) {            return -1;        }        //计算总概率,概率集合累加        double totalRate = 0d;        for (double rate : rates) {            totalRate += rate;        }        // 每个奖品在总概率基础下的概率        List<Double> sortRates = new ArrayList<>();        double everyRate = 0d;        for (double rate : rates) {            everyRate += rate;            sortRates.add(everyRate / totalRate);        }        // 根据区块值来获取抽取到的物品索引        double nextDouble = Math.random();        sortRates.add(nextDouble);        Collections.sort(sortRates);        return sortRates.indexOf(nextDouble);    }}