奖品类:
抽奖逻辑性:
测试类:
测试結果:
产品类:
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
*/
@Data
public 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);
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。