JAVA使用隨機(jī)數(shù)實現(xiàn)概率抽獎
本文實例為大家分享了JAVA使用隨機(jī)數(shù)實現(xiàn)概率抽獎的具體代碼,供大家參考,具體內(nèi)容如下
需求
網(wǎng)站現(xiàn)有一抽獎功能,已經(jīng)定義好獎品,每個獎品都有對應(yīng)的中獎概率。通過獎品概率隨機(jī)進(jìn)行抽獎
實現(xiàn)思路
1、每個獎品都有對應(yīng)的中獎概率,先對所有獎品中獎概率求和
2、計算出每個獎品在0-1之間所占的區(qū)間塊
3、隨機(jī)產(chǎn)生0-1之間的隨機(jī)數(shù),隨機(jī)數(shù)落在哪個區(qū)間,就是中獎哪個
例如現(xiàn)有以下獎品:
獎品A 中獎概率為 0.1
獎品B 中獎概率為 0.01
獎品C 中獎概率為 0.001
獎品D 中獎概率為 0.8
第一步:求出概率總和 0.1+0.01+0.001+0.8 = 0.911
第二步:計算每個獎品的所占區(qū)間塊
獎品A: 0.1 / 0.911 = 0.1098
獎品B: (0.1+0.01)/ 0.911 = 0.1207
獎品C:(0.1+0.11+0.001)/ 0.911 = 0.1218
獎品D:(0.1+0.11+0.001+0.8)/ 0.911 = 1
則:
獎品A的所占區(qū)間為:0~0.1098
獎品B的所占區(qū)間為:0.1098~0.1207
獎品C的所占區(qū)間為:0.1207~0.1218
獎品D的所占區(qū)間為:0.1218~1
代碼如下
/**
* 獎品實體類
*/
public class Award{
public Award(){}
public Award(String awardTitle,double probability){
this.awardTitle = awardTitle;
this.probability = probability;
}
/**獎品ID**/
private String awardId;
/**獎品名**/
private String awardTitle;
/**中獎概率**/
private double probability;
public String getAwardId() {
return awardId;
}
public void setAwardId(String awardId) {
this.awardId = awardId;
}
public String getAwardTitle() {
return awardTitle;
}
public void setAwardTitle(String awardTitle) {
this.awardTitle = awardTitle;
}
public double getProbability() {
return probability;
}
public void setProbability(double probability) {
this.probability = probability;
}
}
public class LotteryUtil {
/**
* 抽獎,獲取中獎獎品
* @param awardList 獎品及中獎概率列表
* @return 中獎商品
*/
public static Award lottery(List<Award> awardList) {
if(awardList.isEmpty()){
throw new AwardListIsEmptyException();
}
//獎品總數(shù)
int size = awardList.size();
//計算總概率
double sumProbability = 0d;
for (Award award : awardList) {
sumProbability += award.getProbability();
}
//計算每個獎品的概率區(qū)間
//例如獎品A概率區(qū)間0-0.1 獎品B概率區(qū)間 0.1-0.5 獎品C概率區(qū)間0.5-1
//每個獎品的中獎率越大,所占的概率區(qū)間就越大
List<Double> sortAwardProbabilityList = new ArrayList<Double>(size);
Double tempSumProbability = 0d;
for (Award award : awardList) {
tempSumProbability += award.getProbability();
sortAwardProbabilityList.add(tempSumProbability / sumProbability);
}
//產(chǎn)生0-1之間的隨機(jī)數(shù)
//隨機(jī)數(shù)在哪個概率區(qū)間內(nèi),則是哪個獎品
double randomDouble = Math.random();
//加入到概率區(qū)間中,排序后,返回的下標(biāo)則是awardList中中獎的下標(biāo)
sortAwardProbabilityList.add(randomDouble);
Collections.sort(sortAwardProbabilityList);
int lotteryIndex = sortAwardProbabilityList.indexOf(randomDouble);
return awardList.get(lotteryIndex);
}
public static void main(String[] args) {
List<Award> awardList = new ArrayList<Award>();
awardList.add(new Award("10個積分",0.35d));
awardList.add(new Award("33個積分",0.25d));
awardList.add(new Award("5元紅包",0.002d));
awardList.add(new Award("20元話費",0.003d));
awardList.add(new Award("京東100元購物卡",0.0005d));
awardList.add(new Award("未中獎",0.1d));
Map<String,Integer> result = new HashMap<String,Integer>();
for(int i=0;i<10000;i++){
Award award = lottery(awardList);
String title = award.getAwardTitle();
Integer count = result.get(title);
result.put(title, count == null ? 1 : count + 1);
}
for (Entry<String, Integer> entry : result.entrySet()) {
System.out.println(entry.getKey() + ", count=" + entry.getValue() +", reate="+ entry.getValue()/10000d);
}
}
}
測試結(jié)果

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Java中l(wèi)ombok的@Builder注解的解析與簡單使用詳解
這篇文章主要介紹了Java中l(wèi)ombok的@Builder注解的解析與簡單使用,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-01-01
Spring?Data?Elasticsearch使用方式(Elasticsearch)
這篇文章主要介紹了Spring?Data?Elasticsearch使用方式(Elasticsearch),具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2025-04-04
springboot調(diào)用python文件的詳細(xì)方案
這篇文章主要為大家詳細(xì)介紹了springboot調(diào)用python文件的詳細(xì)方案,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2025-04-04
Springboot基于websocket實現(xiàn)簡單在線聊天功能
這篇文章主要介紹了Springboot基于websocket實現(xiàn)簡單在線聊天功能,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-06-06

