Assuming that I am using 开发者_JAVA百科the same seed by instantiating a static final Random object with new Random(), is it possible to get the same number twice by calling nextBytes in the same instance?
I am aware that for any given seed, all the possible "random" numbers can be determined, and it is really more like a sequence:
synchronized protected int next(int bits) {
seed = (seed * 0x5DEECE66DL + 0xBL) & ((1L << 48) - 1);
return (int)(seed >>> (48 - bits));
}
So basically if I have this code:
private static final Random random = new Random();
public void doSomething() {
for (int i=0; i < 1000000000; i++) {
byte byteArray[] = new byte[8];
random.nextBytes(byteArray)
}
}
How likely is it that nextBytes will generate the same bytes before it goes thru all the possible numbers that it can generate?.
Would this return the same value before returning all the possible combinations for the given bits?. I am guessing yes, but how often would this happen?.
Class Random
uses a linear congruence generator with a very large period. It does not repeate an int value for a very long time. The call to nextBytes
with an 8-byte array generates two int values and breaks each into four 8-bit values to fill the array.
I believe it is impossible for consecutive calls to nextBytes
to generate the same values. It would mean that the random number generator would have a period of 2. The docs specify a specific behavior for next
that makes this impossible. (A subclass of Random
, of course, can have any kind of pathological behavior you like, but an instance of java.util.Random
will be well-behaved.)
The probability of nextBytes returning the same value that it returned in the previous iteration is exactly the same as the probability of nextBytes returning any specific random eight bytes.
A good random number generator does not make any guarantees about the bits that will be returned other than the fact that the bits will be random. Sometimes it is desirable to have a a generator return all possible values in a random order, but this is not normally the goal of a random generator.
The answers above suggesting that repeated same values cannot occur seem to forget that Java.Random has a period length of 2^48. Because of that, it is entirely possible that nextInt() will generate the exact same integers BEFORE one has gone through all values in the RNG's period. 2^16 times, actually.
Also, as the integers are split in four, the same bytes could (would) appear even if we had to go through all integers. Actually, if that was the case, every byte value would appear 2^24 times before we went through all integer values. I am aware, however, that the original question concerned a byte array consisting of eight bytes. For this case, we would get the same array after 2^31 (2^47 for Java's Random) calls to nextByte (because we need two integers).
We DON'T need to go through all the integers, as i said before.
That being said, if we assume a uniform distribution of the values returned by nextInt(), then the probability of getting the exact same integers in a series of n samples is approximately 1-((2^32 -1) / 2^32) ^(n(n-1)/2). See http://en.wikipedia.org/wiki/Birthday_problem
The number of samples we need to draw to have a probability greater than 50% to have two matching integers is only a little more than 77000. If we now assume we instead uniformly draw a 2^64 number, or two 2^32 integers (for the eight bytes), then we get the same probability after 5*10^9 samples, which is about 2^32. Note that even if by that time, we could have seen all integers, this is still considerably shorter than Random's period. The truth is probably somewhere in-between. Anyway, the probability is very low, but not entirely zero as suggested by posts above.
Am i missing something?
精彩评论