Skip to content
Advertisement

Are there any java.util.random seeds with bad/weird properties? [closed]

I’m curious if there are seeds for java.util.random which have weird/surprising properties. This has little practical use, but I’m still curious.

edit: by weird/surprising properties I mean repeating values or atypical patterns.

Advertisement

Answer

Yes!

The java.util.Random object’s constructor takes in a long as an argument. A bit of sifting through internal classes, we find that this long is processed through the following function:

private static long initialScramble(long seed) {
    return (seed ^ 0x5DEECE66DL) & ((1L << 48) - 1);
}

The goal of this function is to scramble the seed enough so the resulting seed is uniformly distributed across the long range (or something like that).

All we have to do is find an input to this function where it will produce an output of 0 to break things 😊

A bit of brute forcing and I was able to find the long -9223372011639871891 , Using this long as an input, the function returns 0!

Now, we can define a random like the following:

var random = new Random(-9223372011639871891L);

The first call to random.nextInt() will always be zero, no matter the range! Initial calls to other random functions (nextDouble, nextFloat, etc.) will also produce VERY low values (but not exactly zero)

Of course, this all only applies to the first call to the “next” functions, but still cool regardless!

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement