package com.sport.sportactivityserver.common.utils;
import com.sport.sportactivityserver.po.lottery.LyActivityLottery;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StringUtils;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
/**
* create by zdj
* time 2021/11/22
*/
@Slf4j
public class LotteryUtil {
/**
* 抽奖,获取中奖奖品
* @param trailPrizeList 抽奖的奖品列表(里面要有奖品的概率哦)
* @param name 抽奖用户
* @return 中奖商品
*/
public static LyActivityLottery lottery(List<LyActivityLottery> trailPrizeList, String name) {
log.info("/*****************************开始抽奖*************************************/");
if(!StringUtils.isEmpty(name)){
log.info("/****** 抽奖用户名称:"+name);
}
// 先计算出所有商品的总概率
double sumProbability = 0d;
for (LyActivityLottery award : trailPrizeList) {
sumProbability += award.getProbability();
}
log.info("/****** 奖品总概率为:" + sumProbability);
// 计算每个奖品的概率区间
// eg:奖品A概率区间0-0.1 奖品B概率区间 0.1-0.5 奖品C概率区间0.5-1 [每个奖品的中奖率越大,所占的概率区间就越大]
List<Double> sortPrizeProbabilityList = new ArrayList<Double>(trailPrizeList.size());
double tempSumProbability = 0d;
for (LyActivityLottery prize : trailPrizeList) {
tempSumProbability += prize.getProbability(); // 当前商品的概率
sortPrizeProbabilityList.add(tempSumProbability / sumProbability); // 当前商品在总概率中所占的概率权重
}
log.info("/****** 每个奖品对应的概率区间为:" + sortPrizeProbabilityList);
//产生0-总概率之间的随机数
//随机数在哪个概率区间内,则是哪个奖品
double randomDouble = Math.random();
//加入到概率区间中,排序后,返回的下标则是awardList中中奖的下标
sortPrizeProbabilityList.add(randomDouble);
log.info("/****** 随机数添加后【未排序】的区间:" + sortPrizeProbabilityList);
Collections.sort(sortPrizeProbabilityList);
log.info("/****** 随机数添加后【已排序】的区间:" + sortPrizeProbabilityList);
int lotteryIndex = sortPrizeProbabilityList.indexOf(randomDouble);
log.info("/****** 随机数所在的区间的下标:" + lotteryIndex);
log.info("/****** 本次中奖奖品:"+trailPrizeList.get(lotteryIndex).getName());
log.info("/*****************************抽奖结束*************************************/");
return trailPrizeList.get(lotteryIndex);
}
public static void main(String[] args) {
List<LyActivityLottery> trailPrizeList = new ArrayList<>();
LyActivityLottery lyActivityLottery = new LyActivityLottery();
lyActivityLottery.setName("篮球");
lyActivityLottery.setProbability(0.02);
trailPrizeList.add(lyActivityLottery);
LyActivityLottery lyActivityLottery2 = new LyActivityLottery();
lyActivityLottery2.setName("空气");
lyActivityLottery2.setProbability(0.02);
trailPrizeList.add(lyActivityLottery2);
LyActivityLottery lyActivityLottery3 = new LyActivityLottery();
lyActivityLottery3.setName("兵乓球");
lyActivityLottery3.setProbability(0.02);
trailPrizeList.add(lyActivityLottery3);
LyActivityLottery lyActivityLottery4 = new LyActivityLottery();
lyActivityLottery4.setName("足球");
lyActivityLottery4.setProbability(0.02);
trailPrizeList.add(lyActivityLottery4);
LyActivityLottery lyActivityLottery5 = new LyActivityLottery();
lyActivityLottery5.setName("羽毛球");
lyActivityLottery5.setProbability(0.02);
trailPrizeList.add(lyActivityLottery5);
lottery(trailPrizeList, "系统");
}
}
测试:
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/137488.html