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

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

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

服務(wù)器之家 - 編程語言 - JAVA教程 - java隨機(jī)數(shù)生成具體實(shí)現(xiàn)代碼

java隨機(jī)數(shù)生成具體實(shí)現(xiàn)代碼

2020-04-20 13:57賣蠟筆的小新 JAVA教程

這篇文章主要為大家分享了java隨機(jī)數(shù)生成具體實(shí)現(xiàn)代碼,具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了java隨機(jī)數(shù)生成代碼,供大家參考,具體內(nèi)容如下

?
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package com.gonvan.common.utils;
 
import java.util.*;
 
/**
 * 隨機(jī)數(shù)工具
 *
 * @author yuerzm
 *  
 */
public final class LotteryAliasMethod {
 
  /**
   * The random number generator used to sample from the distribution.
   */
  private final Random  random;
 
  /**
   * The alias tables.
   */
  private final int[]   alias;
 
  /**
   * The probability tables.
   */
  private final double[] probability;
 
  /**
   * Constructs a new AliasMethod to sample from a discrete distribution and
   * hand back outcomes based on the probability distribution.
   * <p/>
   * Given as input a list of probabilities corresponding to outcomes 0, 1,
   * ..., n - 1, this constructor creates the probability and alias tables
   * needed to efficiently sample from this distribution.
   *
   * @param probabilities
   *      The list of probabilities.
   */
  public LotteryAliasMethod(List<Double> probabilities) {
    this(probabilities, new Random());
  }
 
  /**
   * Constructs a new AliasMethod to sample from a discrete distribution and
   * hand back outcomes based on the probability distribution.
   * <p/>
   * Given as input a list of probabilities corresponding to outcomes 0, 1,
   * ..., n - 1, along with the random number generator that should be used as
   * the underlying generator, this constructor creates the probability and
   * alias tables needed to efficiently sample from this distribution.
   *
   * @param probabilities
   *      The list of probabilities.
   * @param random
   *      The random number generator
   */
  public LotteryAliasMethod(List<Double> probabilities, Random random) {
    /* Begin by doing basic structural checks on the inputs. */
    if (probabilities == null || random == null)
      throw new NullPointerException();
    if (probabilities.size() == 0)
      throw new IllegalArgumentException("Probability vector must be nonempty.");
 
    /* Allocate space for the probability and alias tables. */
    probability = new double[probabilities.size()];
    alias = new int[probabilities.size()];
 
    /* Store the underlying generator. */
    this.random = random;
 
    /* Compute the average probability and cache it for later use. */
    final double average = 1.0 / probabilities.size();
 
    /*
     * Make a copy of the probabilities list, since we will be making
     * changes to it.
     */
    probabilities = new ArrayList<Double>(probabilities);
 
    /* Create two stacks to act as worklists as we populate the tables. */
    Deque<Integer> small = new ArrayDeque<Integer>();
    Deque<Integer> large = new ArrayDeque<Integer>();
 
    /* Populate the stacks with the input probabilities. */
    for (int i = 0; i < probabilities.size(); ++i) {
      /*
       * If the probability is below the average probability, then we add
       * it to the small list; otherwise we add it to the large list.
       */
      if (probabilities.get(i) >= average)
        large.add(i);
      else
        small.add(i);
    }
 
    /*
     * As a note: in the mathematical specification of the algorithm, we
     * will always exhaust the small list before the big list. However,
     * due to floating point inaccuracies, this is not necessarily true.
     * Consequently, this inner loop (which tries to pair small and large
     * elements) will have to check that both lists aren't empty.
     */
    while (!small.isEmpty() && !large.isEmpty()) {
      /* Get the index of the small and the large probabilities. */
      int less = small.removeLast();
      int more = large.removeLast();
 
      /*
       * These probabilities have not yet been scaled up to be such that
       * 1/n is given weight 1.0. We do this here instead.
       */
      probability[less] = probabilities.get(less) * probabilities.size();
      alias[less] = more;
 
      /*
       * Decrease the probability of the larger one by the appropriate
       * amount.
       */
      probabilities.set(more, (probabilities.get(more) + probabilities.get(less)) - average);
 
      /*
       * If the new probability is less than the average, add it into the
       * small list; otherwise add it to the large list.
       */
      if (probabilities.get(more) >= 1.0 / probabilities.size())
        large.add(more);
      else
        small.add(more);
    }
 
    /*
     * At this point, everything is in one list, which means that the
     * remaining probabilities should all be 1/n. Based on this, set them
     * appropriately. Due to numerical issues, we can't be sure which
     * stack will hold the entries, so we empty both.
     */
    while (!small.isEmpty())
      probability[small.removeLast()] = 1.0;
    while (!large.isEmpty())
      probability[large.removeLast()] = 1.0;
  }
 
  /**
   * Samples a value from the underlying distribution.
   *
   * @return A random value sampled from the underlying distribution.
   */
  public int next() {
    /* Generate a fair die roll to determine which column to inspect. */
    int column = random.nextInt(probability.length);
 
    /* Generate a biased coin toss to determine which option to pick. */
    boolean coinToss = random.nextDouble() < probability[column];
 
    /* Based on the outcome, return either the column or its alias. */
    return coinToss ? column : alias[column];
  }
 
}

 

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助。

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 天堂精品高清1区2区3区 | 日韩a无吗一区二区三区 | 黄a在线观看 | 国产成人亚洲综合a∨婷婷 国产成人亚洲精品乱码在线观看 | 射逼网| 久久精品视在线观看85 | 国产福利不卡一区二区三区 | 国产香蕉国产精品偷在线观看 | 99精品久久精品一区二区 | 成人榴莲视频 | 亚洲人影院| 喜爱夜蒲2三级做爰 | 美女被的在线网站91 | 国产片自拍 | 久久这里只有精品视频9 | 免费观看成年人视频 | 秋霞午夜视频在线观看 | 久久久久久久久女黄 | 精品国产爱久久 | 深夜免费在线视频 | 国色天香社区在线视频免费观看 | 无套日出白浆在线播放 | 国产偷窥女洗浴在线观看亚洲 | 国产成人精品.一二区 | 贰佰麻豆剧果冻传媒一二三区 | 短篇艳妇系列 | 亚洲欧美自偷自拍另类小说 | 亚洲va欧美va国产va天堂影 | 糖心视频在线观看 | 高清欧美videossexo免费 | 美女和男人差差 | 国产精品日本亚洲777 | 99久久国产综合精品女小说 | 亚洲咪咪 | 好涨好大我快受不了了视频网 | 青青热久免费精品视频精品 | 大吊小说| 免费在线观看伦理片 | 精品一区二区91 | 欧美午夜精品 | 四虎成人免费大片在线 |