Java 随机均匀分布
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4253500/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Uniform distribution with Random
提问by DaHymanal
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?
我知道如果我使用 Java 的 Random 生成器,用 nextInt 生成数字,数字将均匀分布。但是如果我使用 2 个 Random 实例,用两个 Random 类生成数字会发生什么。数字是否均匀分布?
采纳答案by Grodriguez
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.
每个Random
实例生成的数字是均匀分布的,所以如果你把两个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
:
请注意,即使结果分布是均匀的,您也可能需要注意种子以避免两个生成器的输出之间存在相关性。如果您使用默认的无参数构造函数,则种子应该已经不同了。从源代码java.util.Random
:
private static volatile long seedUniquifier = 8682522807148012L;
public Random() { this(++seedUniquifier + System.nanoTime()); }
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.
如果要显式设置种子(通过使用Random(long seed)
构造函数或调用setSeed(long seed)
),则需要自己处理。一种可能的方法是使用随机数生成器为所有其他生成器生成种子。
回答by Mike Clark
Well, if you seed both Random
instances with the same value, you will definitely not get quality discrete uniform distribution. Consider the most basic case, which literally prints the exact same number twice (doesn't get much less random than that ...):
好吧,如果你Random
用相同的值播种两个实例,你肯定不会得到高质量的离散均匀分布。考虑最基本的情况,它从字面上打印两次完全相同的数字(不会比那少得多......):
public class RngTest2 {
public static void main(String[] args) throws Exception {
long currentTime = System.currentTimeMillis();
Random r1 = new Random(currentTime);
Random r2 = new Random(currentTime);
System.out.println(r1.nextInt());
System.out.println(r2.nextInt());
}
}
But that's just a single iteration. What happens if we start cranking up the sample size?
但这只是一次迭代。如果我们开始增加样本量会发生什么?
Here is a scatter plot of a distribution from running two same-seeded RNGs side-by-side to generate 2000 numbers total:
这是并排运行两个相同种子 RNG 以生成总共 2000 个数字的分布散点图:
And here is a distribution of running a single RNG to generate 2000 numbers total:
这是运行单个 RNG 以生成总共 2000 个数字的分布:
It seems pretty clear which approach produced higher quality discrete uniform distribution over this finite set.
似乎很清楚哪种方法在这个有限集合上产生了更高质量的离散均匀分布。
Now almost everyone knows that seeding two RNGs with the same seed is a bad idea if you're looking for high quality randomness. But this case does make you stop and think: we havecreated a scenario where each RNG is independently emitting fairly high quality randomness, but when their output is combined it is notably lower in quality (less discrete.)
现在几乎每个人都知道,如果您正在寻找高质量的随机性,用相同的种子播种两个 RNG 是一个坏主意。但是,这种情况下,确实让你停下来想一想:我们已经创造了一个场景,每一个RNG独立发射相当高品质的随机性,但是当他们的输出组合是在质量显着降低(较少离散的。)