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

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

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

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

Java抽獎(jiǎng)算法第二例

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

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

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

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

Java抽獎(jiǎng)算法第二例

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;
    // 洗牌,打亂獎(jiǎng)品次序
    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之間的一個(gè)隨機(jī)數(shù)
    int luckyNumber = new Random().nextInt(mulriple);
    int luckyPrizeId = 0;
    // 查找隨機(jī)數(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) {
      // 獎(jiǎng)品庫(kù)存減一
    }
 
    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 {
 
  /**
   * 獎(jiǎng)品唯一標(biāo)示
   */
  private Integer prizeId;
 
  /**
   * 中獎(jiǎng)概率
   */
  private BigDecimal probability;
 
  /**
   * 獎(jiǎng)品數(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. 測(cè)試

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("抽獎(jiǎng)次數(shù)" + times);
    System.out.println("prize1中獎(jiǎng)次數(shù)" + prize1GetTimes);
    System.out.println("prize2中獎(jiǎng)次數(shù)" + prize2GetTimes);
    System.out.println("prize3中獎(jiǎng)次數(shù)" + prize3GetTimes);
    System.out.println("prize4中獎(jiǎng)次數(shù)" + prize4GetTimes);
    System.out.println("prize5中獎(jiǎng)次數(shù)" + prize5GetTimes);
  }
}

結(jié)果:

Java抽獎(jiǎng)算法第二例

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

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

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 边吃胸边膜下刺激免费男对女 | xxxx成人| 欧美一级高清片 | 欧美综合色网 | 久热人人综合人人九九精品视频 | ova巨公主催眠1在线观看 | 欧美三级小视频 | 楚乔传第二部免费播放电视连续剧 | 午夜性色一区二区三区不卡视频 | 国产精品香蕉 | 亚洲天堂成人在线观看 | 青草视频在线观看免费资源 | 香蕉国产成版人视频在线观看 | 青视频在线 | 久久国产乱子伦免费精品 | 日本videosdesexo乱| 海角社区在线登录 | 国产青草视频在线观看免费影院 | 久久视频这只精品99re6 | 亚洲第一网色综合久久 | uoco福利姬网站 | 国产亚洲欧美在线中文bt天堂网 | 日本色午夜| 久99视频精品免费观看福利 | 香蕉久久久久久狠狠色 | 成人α片 | 好爽好深好猛好舒服视频上 | 欧美se图 | 久久91精品国产91久 | sao虎在线精品永久 s0e一923春菜花在线播放 | 亚洲国产成人久久精品hezyo | 成人一区二区免费中文字幕 | 国产美女在线一区二区三区 | 国产高清路线一路线二2022 | 性刺激欧美三级在线现看中文 | 男人j桶进女人p桶爽 | 国产探花在线视频 | 亚洲天堂色图 | 亚洲午夜精品久久久久 | 精品日韩欧美一区二区三区在线播放 | 久久偷拍人 |