John B. Matthews wrote:
Random rand1 = new Random(seed);
Random rand2 = new Random(rand1.nextInt());
Random rand3 = new Random(rand1.nextInt());
This can result in all generators having the same seed.
How so? Assuming that Random is reasonably good, which the default
Random is, then the seed, the first int generated and the second int
generated are all different.
All implementations use a particular linear congruential pseudorandom
number generator, as required by the class's contract. Even though
nextInt() is optimized to select from the high-order bits, a repeat
could occur. Instead, use nextLong():
Random rand1 = new Random(seed);
Random rand2 = new Random(rand1.nextLong());
Random rand3 = new Random(rand1.nextLong());
Would this ever repeat for period > 2?
The docs state that:
The method nextLong is implemented by class Random as if by:
public long nextLong() {
return ((long)next(32) << 32) + next(32);
}
so it is not obvious that it could not happen.
You will need to either consult a good mathematician or
try for all values of seed whether it happens.
Ah, I see your point. So the probability is very low (1 in 2^32), but
not zero. Then the question is why one might need three uncorrelated
sequences.
The probability is either very low or zero. The algorithm of
java.util.Random isgiven. You will need to either consult
someone that really understand the math behind LCG or
simply try it with 2^64 (or 2^48 if you can reduce to that)
seed values to know for sure.
I have to agree. The given lcg [x = a + bx (mod m); a = 11, b =
25214903917, m = 2^48] is known to have period 2^48. The difficulty here
is that it's also known to have problems when used to get parallel
streams of pseudorandom numbers, as the op proposed. My point would be
that parallel sequences are a bigger problem than the probability of
hitting the cycle length.
In fact, running two generators in parallel is the basis of Floyd's
cycle length algorithm. Knowing the period is 2^48, at 2Ghz... Looks
like trying it might take a while