Java – random and uniform distribution

I know that if I use the random generator in Java and generate numbers with nextint, the numbers will be evenly distributed However, if I use two random instances, two random class numbers will occur Will these figures be uniformly distributed?

Solution

The numbers generated by each random instance will be evenly distributed, so if the sequences of random numbers generated by two random instances are combined, they should also be evenly distributed

Note that even if the resulting distribution is uniform, you may need to pay attention to seeds to avoid correlation between the outputs of the two generators If you use the default parameterless constructor, the seed should already be different From Java util. Random's source code

private static volatile long seedUniquifier = 8682522807148012L;

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

If you explicitly set the seed (by using the random (long seed) constructor or calling setseed (long seed)), you need to handle it yourself One possible approach is to use a random number generator to generate the seeds of all other generators

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>