时间:2021-05-19
本文实例为大家分享了java随机数生成代码,供大家参考,具体内容如下
package com.gonvan.common.utils; import java.util.*; /** * 随机数工具 * * @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) { if (probabilities == null || random == null) throw new NullPointerException(); if (probabilities.size() == 0) throw new IllegalArgumentException("Probability vector must be nonempty."); probability = new double[probabilities.size()]; alias = new int[probabilities.size()]; this.random = random; 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); Deque<Integer> small = new ArrayDeque<Integer>(); Deque<Integer> large = new ArrayDeque<Integer>(); 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()) { 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() { int column = random.nextInt(probability.length); boolean coinToss = random.nextDouble() < probability[column]; return coinToss ? column : alias[column]; } }以上就是本文的全部内容,希望对大家的学习有所帮助。
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
本文实例为大家分享了Java实现生成n个不重复的随机数的具体代码,供大家参考,具体内容如下需求:根据min和max,生成n个不重复的随机数。(注:范围[min,
本文实例为大家分享了java实现随机数生成器的具体代码,供大家参考,具体内容如下自己编的随机数生成器,比较简陋,功能也单一,当作练手。Application.j
js生成1到100的随机数js生成随机数使用math.random()函数Math.random()具体实现:1、定义一个random()函数,原理是随机数和最
本文介绍了ASP生成不重复随机数的方法,asp生成不重复随机数的实例代码,有需要的朋友参考下。 例1,ASP生成随机数,很简单: 代码示例:Random
Java生成验证码的流程是:收到请求->生成验证码所用的随机数->使用随机数写出图片->将随机数记录到Session中->输出验证码Java验证验证码的流程是: