The next method of java.util.Random, when I view source in Eclipse, is essentially:
seed = (seed * 0x5DEECE66DL + 0xBL) & ((1L << 48) - 1);
return (int)(seed >>> (48 - bits));
How can I determine if different JDKs or JVMs will implement next in a different way or with different constants?
I have already come across different no-argument constructors in Weird behaviour when seeding Java Random. I want to know if a similar thing might occur with the next method. Where can I find the source to different implementations?
The Javadoc for
Random.next()explicitly states which algorithm is used to generate the next number.It is theoretically possible that a different JVM might use a different algorithm, but this is unlikely, especially if you restrict yourself to JVM’ that are based on the Sun / Oracle libraries.
You’d need to check to be sure, but probably yes. This is the kind of thing that Sun / Oracle are unlikely to change because of the potential for breaking lots of existing Java applications and test suites written over that last 15 or so years.
Here are some facts:
the javadoc for Java 1.3.1 through Java 1.7 (*) contains the exact same specification for this method,
an implementation is only permitted to use the trademark “Java” if it conforms to the specification
most (if not all) Java(TM) implementations use class libraries that are derived from the Oracle / Sun source code,
developers would notice that a different algorithm was used … and complain loudly.
So the likelihood of a Java(TM) implementation using a different algorithm is pretty small.
(* I couldn’t find the Java 1.1 javadocs online, but I expect that they will say the same thing.)