随机分布均匀 [英] Uniform distribution with Random

查看:75
本文介绍了随机分布均匀的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道如果我使用Java的Random生成器,并使用nextInt生成数字,则数字将均匀分布.但是,如果我使用2个Random实例,并使用两个Random类生成数字,会发生什么情况.这些数字是否会均匀分布?

I know if i use the Random generator from Java, generating numbers with nextInt, the numbers will be uniformly distributed. But what happens if I use 2 instances of Random, generating numbers with the both Random classes. The numbers will be uniformly distributed or not?

推荐答案

每个Random实例生成的数字将均匀分布,因此,如果将两个Random实例生成的随机数字序列组合在一起,则它们应该也要均匀分布.

The numbers generated by each Random instance will be uniformly distributed, so if you combine the sequences of random numbers generated by both Random instances, they should be uniformly distributed too.

请注意,即使结果分布是均匀的,您也可能要注意种子,以避免两个生成器的输出之间存在相关性.如果使用默认的no-arg构造函数,则种子应该已经不同.从java.util.Random的源代码:

Note that even if the resulting distribution is uniform, you might want to pay attention to the seeds to avoid correlation between the output of the two generators. If you use the default no-arg constructor, the seeds should already be different. From the source code of java.util.Random:

private static volatile long seedUniquifier = 8682522807148012L;

public Random() { this(++seedUniquifier + System.nanoTime()); }

如果要显式设置种子(通过使用Random(long seed)构造函数或调用setSeed(long seed)),则需要自己进行处理.一种可能的方法是使用随机数生成器为所有其他生成器生成种子.

If you are setting the seed explicitly (by using the Random(long seed) constructor, or calling setSeed(long seed)), you'll need to take care of this yourself. One possible approach is to use a random number generator to produce the seeds for all other generators.

这篇关于随机分布均匀的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆