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?
nextInt(int n) : The nextInt(int n) is used to get a random number between 0(inclusive) and the number passed in this argument(n), exclusive.
Java Random Generation - JavaBitsNotebook.com. Computers can be used to simulate the generation of random numbers. This random generation is referred to as a pseudo-random generation. These created values are not truly "random" because a mathematical formula is used to generate the values.
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.
Does that documentation necessarily apply to all JDKs?
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.)
How can I determine if different JDKs or JVMs will implement next in a different way or with different constants?
By using the same seed in a program on different JVMs and seeing if you get the same sequence, as the sequence is deterministic for a given seed.
I'm not sure JDKs are allowed to use a different algorithm, though, as the documentation for next
says:
The method
next
is implemented by classRandom
by atomically updating the seed to
(seed * 0x5DEECE66DL + 0xBL) & ((1L << 48) - 1)
and returning
(int)(seed >>> (48 - bits)).
This is a linear congruential pseudorandom number generator, as defined by D. H. Lehmer and described by Donald E. Knuth in The Art of Computer Programming, Volume 3: Seminumerical Algorithms, section 3.2.1.
...which seems to set a specific contract, although just before that it says
The general contract of
next
is that it returns anint
value and if the argumentbits
is between1
and32
(inclusive), then that many low-order bits of the returned value will be (approximately) independently chosen bit values, each of which is (approximately) equally likely to be0
or1
.
...so perhaps that's allowing wiggle room on the algorithm.
Where can I find the source to different implementations?
If they're open source, presumably you can find the implementation in the project's repo. If they're closed, well...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With