java_java随机数生成具体实现代码,本文实例为大家分享了java随机
n - 1, (probabilities.get(more) + probabilities.get(less)) - average);/IT之家IT之家 If the new probability is less than the average, 1, weIT之家 will always exhaust the small list before the big list. However, n - 1, this is not necessarily true.IT之家 Consequently, then we addIT之家 it to the small list; otherwise we add it to the large list.IT之家/if (probabilities.get(i) = average)large.add(i);elsesmall.add(i);}/IT之家IT之家 As a note: in the mathematical specification of the algorithm, which means that theIT之家 remaining probabilities should all be 1/n. Based on this, this constructor creates the probability and alias tablesIT之家 needed to efficiently sample from this distribution.IT之家IT之家 @param probabilitiesIT之家The list of probabilities.IT之家/ public LotteryAliasMethod(ListDouble probabilities) {this(probabilities, this inner loop (which tries to pair small and largeIT之家 elements) will have to check that both lists aren't empty.IT之家/while (!small.isEmpty() !large.isEmpty()) {/IT之家 Get the index of the small and the large probabilities. IT之家/int less = small.removeLast();int more = large.removeLast();/IT之家IT之家 These probabilities have not yet been scaled up to be such thatIT之家 1/n is given weight 1.0. We do this here instead.IT之家/probability[less] = probabilities.get(less) IT之家 probabilities.size();alias[less] = more;/IT之家IT之家 Decrease the probability of the larger one by the appropriateIT之家 amount.IT之家/probabilities.set(more。
we can't be sure whichIT之家 stack will hold the entries,IT之家 due to floating point inaccuracies。
package com.gonvan.common.utils;import java.util.IT之家;/IT之家IT之家 IT之家 随机数工具 IT之家 IT之家 @author yuerzm IT之家IT之家/public final class LotteryAliasMethod {/IT之家IT之家IT之家 The random number generator used to sample from the distribution.IT之家/ private final Random random;/IT之家IT之家IT之家 The alias tables.IT之家/ private final int[] alias;/IT之家IT之家IT之家 The probability tables.IT之家/ private final double[] probability;/IT之家IT之家IT之家 Constructs a new AliasMethod to sample from a discrete distribution andIT之家 hand back outcomes based on the probability distribution.IT之家 p/IT之家 Given as input a list of probabilities corresponding to outcomes 0。
new Random()); }/IT之家IT之家IT之家 Constructs a new AliasMethod to sample from a discrete distribution andIT之家 hand back outcomes based on the probability distribution.IT之家 p/IT之家 Given as input a list of probabilities corresponding to outcomes 0, 1, Random random) {/IT之家 Begin by doing basic structural checks on the inputs. IT之家/if (probabilities == null || random == null)throw new NullPointerException();if (probabilities.size() == 0)throw new IllegalArgumentException("Probability vector must be nonempty.");/IT之家 Allocate space for the probability and alias tables. IT之家/probability = new double[probabilities.size()];alias = new int[probabilities.size()];/IT之家 Store the underlying generator. IT之家/this.random = random;/IT之家 Compute the average probability and cache it for later use. IT之家/final double average = 1.0 / probabilities.size();/IT之家IT之家 Make a copy of the probabilities list, everything is in one list, since we will be makingIT之家 changes to it.IT之家/probabilities = new ArrayListDouble(probabilities);/IT之家 Create two stacks to act as worklists as we populate the tables. IT之家/DequeInteger small = new ArrayDequeInteger();DequeInteger large = new ArrayDequeInteger();/IT之家 Populate the stacks with the input probabilities. IT之家/for (int i = 0; i probabilities.size(); ++i) {/IT之家IT之家 If the probability is below the average probability,IT之家 ...,IT之家 ..., return either the column or its alias. IT之家/return coinToss column : alias[column]; }} , so we empty both.IT之家/while (!small.isEmpty())probability[small.removeLast()] = 1.0;while (!large.isEmpty())probability[large.removeLast()] = 1.0; }/IT之家IT之家IT之家 Samples a value from the underlying distribution.IT之家IT之家 @return A random value sampled from the underlying distribution.IT之家/ public int next() {/IT之家 Generate a fair die roll to determine which column to inspect. IT之家/int column = random.nextInt(probability.length);/IT之家 Generate a biased coin toss to determine which option to pick. IT之家/boolean coinToss = random.nextDouble() probability[column];/IT之家 Based on the outcome。
this constructor creates the probability andIT之家 alias tables needed to efficiently sample from this distribution.IT之家IT之家 @param probabilitiesIT之家The list of probabilities.IT之家 @param randomIT之家The random number generatorIT之家/ public LotteryAliasMethod(ListDouble probabilities, add it into theIT之家 small list; otherwise add it to the large list.IT之家/if (probabilities.get(more) = 1.0 / probabilities.size())large.add(more);elsesmall.add(more);}/IT之家IT之家 At this point, along with the random number generator that should be used asIT之家 the underlying generator, set themIT之家 appropriately. Due to numerical issues,。
相关热词:
本站内容来源于网络,如有侵权请与我们联系,我们会及时删除,我们深感抱歉!
注:本站所有信息仅供用于网络技术学习参考,学习中请遵循相关法律法规!
本文地址: https://v30.fanwenzhu.com/jiaob/java/9078.shtml
相关文章
热门TAG
win10 ecshop 主机 阿里云 解决 配置 C# C++ 解析 SQL语句 命令 Go语言 方法 CSS3 HTML5 CSS win7 MSSQL 服务器配置 IIS7.5 IIS7 IIS6 IIS CentOS 7 Linux oracle数据库 oracle phpcms discuz discuz教程最新文章
-
Fitness fitness){ /*double X1=m
时间:2021-01-21
-
所以这里也是需要注意的
时间:2021-01-21
-
hadoop上传文件成果实例代
时间:2021-01-15
-
hadoop负责按key值将map的输
时间:2021-01-15
-
记得勾选springconfig.xml 因为
时间:2021-01-14
-
如果当前没有事务
时间:2021-01-14
-
SpringCloud整合Nacos实现流程
时间:2021-01-07
-
Intellijidea建javaWeb以及Ser
时间:2021-01-07
热门文章
-
Java内部类的实现原理与可能的内存泄漏说
时间:2020-12-29
-
记得勾选springconfig.xml 因为我们之前下载
时间:2021-01-14
-
SpringCloud整合Nacos实现流程详解
时间:2021-01-07
-
JAVA多线程和并发基础面试问答(翻译)
时间:2020-12-25
-
Spring Boot 使用Druid详解
时间:2020-12-28
-
多方位解析,2020Java开发就业前景怎么样
时间:2020-12-25
-
最新IDEA永久激活教程(支持最新2019.2版本
时间:2020-12-25
-
Fitness fitness){ /*double X1=min+0.382*(max-min);*
时间:2021-01-21
-
详解SpringMVC在IDEA中的第一个程序
时间:2021-01-06
-
Java基础:集合框架
时间:2020-12-28
