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

多条告白如次剧本只需引入一次
奖品类:
抽奖论理:
尝试类:
尝试截止:
商品类:
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;}}尝试类:
【java抽奖系统的设计java抽奖程序代码】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 = https://www.juguize.com/b/countMap.get(key);countMap.put(key, value == null ? 1 : value + 1);}for (Map.Entry 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);}}

    推荐阅读