一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

PHP教程|ASP.NET教程|JAVA教程|ASP教程|

服務(wù)器之家 - 編程語言 - JAVA教程 - Java抽獎算法第二例

Java抽獎算法第二例

2020-06-02 11:13go2shell JAVA教程

這篇文章主要為大家詳細(xì)介紹了Java抽獎算法,根據(jù)概率將獎品劃分區(qū)間,每個區(qū)間代表一個獎品,然后抽取隨機數(shù),具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實例為大家分享了java抽獎算法,供大家參考,具體內(nèi)容如下

1. 算法分析
 根據(jù)概率將獎品劃分區(qū)間,每個區(qū)間代表一個獎品,然后抽取隨機數(shù),反查落在那個區(qū)間上,即為所抽取的獎品。 

Java抽獎算法第二例

2. 代碼
核心算法 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
public class Arithmetic {
  // 放大倍數(shù)
  private static final int mulriple = 1000000;
 
  public int pay(List<Prize> prizes) {
    int lastScope = 0;
    // 洗牌,打亂獎品次序
    Collections.shuffle(prizes);
    Map<Integer, int[]> prizeScopes = new HashMap<Integer, int[]>();
    Map<Integer, Integer> prizeQuantity = new HashMap<Integer, Integer>();
    for (Prize prize : prizes) {
      int prizeId = prize.getPrizeId();
      // 劃分區(qū)間
      int currentScope = lastScope + prize.getProbability().multiply(new BigDecimal(mulriple)).intValue();
      prizeScopes.put(prizeId, new int[] { lastScope + 1, currentScope });
      prizeQuantity.put(prizeId, prize.getQuantity());
 
      lastScope = currentScope;
    }
 
    // 獲取1-1000000之間的一個隨機數(shù)
    int luckyNumber = new Random().nextInt(mulriple);
    int luckyPrizeId = 0;
    // 查找隨機數(shù)所在的區(qū)間
    if ((null != prizeScopes) && !prizeScopes.isEmpty()) {
      Set<Entry<Integer, int[]>> entrySets = prizeScopes.entrySet();
      for (Map.Entry<Integer, int[]> m : entrySets) {
        int key = m.getKey();
        if (luckyNumber >= m.getValue()[0] && luckyNumber <= m.getValue()[1] && prizeQuantity.get(key) > 0) {
          luckyPrizeId = key;
          break;
        }
      }
    }
 
    if (luckyPrizeId > 0) {
      // 獎品庫存減一
    }
 
    return luckyPrizeId;
  }
}

Prize bean

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
public class Prize {
 
  /**
   * 獎品唯一標(biāo)示
   */
  private Integer prizeId;
 
  /**
   * 中獎概率
   */
  private BigDecimal probability;
 
  /**
   * 獎品數(shù)量
   */
  private Integer quantity;
 
  public Integer getPrizeId() {
    return prizeId;
  }
 
  public void setPrizeId(Integer prizeId) {
    this.prizeId = prizeId;
  }
 
  public BigDecimal getProbability() {
    return probability;
  }
 
  public void setProbability(BigDecimal probability) {
    this.probability = probability;
  }
 
  public Integer getQuantity() {
    return quantity;
  }
 
  public void setQuantity(Integer quantity) {
    this.quantity = quantity;
  }
 
}

3. 測試

prize1概率: 5% 
prize2概率: 10% 

prize3概率: 15% 
prize4概率: 20% 

prize5概率: 50% 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
public class Test {
  public static void main(String[] args) {
    List<Prize> prizes = new ArrayList<Prize>();
    Prize prize1 = new Prize();
    prize1.setPrizeId(1);
    prize1.setProbability(new BigDecimal(0.05));
    prize1.setQuantity(1);
    prizes.add(prize1);
 
    Prize prize2 = new Prize();
    prize2.setPrizeId(2);
    prize2.setProbability(new BigDecimal(0.10));
    prize2.setQuantity(10);
    prizes.add(prize2);
 
    Prize prize3 = new Prize();
    prize3.setPrizeId(3);
    prize3.setProbability(new BigDecimal(0.15));
    prize3.setQuantity(20);
    prizes.add(prize3);
 
    Prize prize4 = new Prize();
    prize4.setPrizeId(4);
    prize4.setProbability(new BigDecimal(0.20));
    prize4.setQuantity(50);
    prizes.add(prize4);
 
    Prize prize5 = new Prize();
    prize5.setPrizeId(5);
    prize5.setProbability(new BigDecimal(0.50));
    prize5.setQuantity(200);
    prizes.add(prize5);
 
    int prize1GetTimes = 0;
    int prize2GetTimes = 0;
    int prize3GetTimes = 0;
    int prize4GetTimes = 0;
    int prize5GetTimes = 0;
    Arithmetic arithmetic = new Arithmetic();
    int times = 1000;
    for (int i = 0; i < times; i++) {
      int prizeId = arithmetic.pay(prizes);
      switch (prizeId) {
        case 1:
          prize1GetTimes++;
          break;
        case 2:
          prize2GetTimes++;
          break;
        case 3:
          prize3GetTimes++;
          break;
        case 4:
          prize4GetTimes++;
          break;
        case 5:
          prize5GetTimes++;
          break;
      }
    }
    System.out.println("抽獎次數(shù)" + times);
    System.out.println("prize1中獎次數(shù)" + prize1GetTimes);
    System.out.println("prize2中獎次數(shù)" + prize2GetTimes);
    System.out.println("prize3中獎次數(shù)" + prize3GetTimes);
    System.out.println("prize4中獎次數(shù)" + prize4GetTimes);
    System.out.println("prize5中獎次數(shù)" + prize5GetTimes);
  }
}

結(jié)果:

Java抽獎算法第二例

通過1000次抽取,我們看出算法精度還是很高的。

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 96免费精品视频在线 | aaa级黄色片| 久久re这里精品23 | 免费一区二区视频 | 三上悠亚久久国产 | 99精彩视频在线观看 | 毛片大全免费看 | blacked最大的吊| 99热久久这里只有精品6国产网 | 美女的隐私脱裤子无遮挡 | h在线动漫 | 性做久久久久免费观看 | 色综合天天综合中文网 | 午夜第一页 | 国产精品天天影视久久综合网 | 国产日韩高清一区二区三区 | 亚洲国产成人久久综合一区 | 高清在线免费观看 | 亚洲国产果果在线播放在线 | 国产福利资源 | 99精品视频免费 | www一级片| 草莓视频旧版 | 日韩理论片在线看免费观看 | 美女和男人一起差差 | 美女黄板视频 | 国产成人精品三级在线 | 亚洲六月丁香婷婷综合 | 成年人免费在线看 | 亚洲经典激情春色另类 | 99久久精品免费看国产情侣 | 欧美日韩国产精品va | 蜜桃视频在线观看www | 国产综合亚洲欧美日韩一区二区 | 国产伦精品一区二区三区免费迷 | 2020国产精品视频 | 亚洲成年人免费网站 | 天天做天天爱天天综合网 | 欧美成人中文字幕在线看 | 亚洲天堂精品在线观看 | 久久精品视频uu |