java

推荐列表 站点导航

当前位置:首页 > 脚本编程 > java >

java_java随机数生成具体实现代码,本文实例为大家分享了java随机

来源:网络整理  作者:fen  发布时间:2020-12-25 22:31
java随机数生成具体实现代码,本文实例为大家分享了java随机数生成代码,供大家参考,具体内容如下package com.gonvan....

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

Copyright © www.juheyunku.com      关于 | 合作 | 声明 | 联系 | 更新 | 地图 | Tags

java_java随机数生成具体实现代码,本文实例为大家分享了java随机

2020-12-25 编辑:fen

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

相关文章

风云图片

推荐阅读

返回java频道首页