So basically the nextBoolean method can only return true or false . And the total number of seed values โโcan be [Long.MIN_VALUE, Long.MAX_VALUE] . So, you can assume that for half of these seeds you get true , and for the other half you get false .
Now that you are repeating 10 numbers, it is possible that for these 10 seeds, the value you get is true . When you try to use a larger range, you are more likely to get an equal distribution of both values.
Now every time you call nextBoolean() , the seed is updated to some other value using (seed * 0x5DEECE66DL + 0xBL) & ((1L << 48) - 1) . Therefore, if the current seed is 1 , the next seed will be 25214903916 , where you can get true or false (which you do not know). This is why you sometimes get false when you call nextBoolean() twice in a loop. After all, it is a pseudo random number generator.
By the way, you really don't need to call the setSeed() method. This method is only used to reset for seed for a specific value. An instance of the Random class will start from the initial value and update it every time you get a value from it. You do not need to worry about it.
If you see the code of the Random class, so they assign the seed for the first time:
public Random() { this(seedUniquifier() ^ System.nanoTime()); } private static long seedUniquifier() {
So, you should leave the task just for that.
source share