Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does java.util.Random implementation differ between JREs or platforms?

Tags:

java

random

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?

like image 422
Flash Avatar asked Mar 28 '12 12:03

Flash


People also ask

Is nextInt inclusive or exclusive?

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.

Is Java random really random?

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.


2 Answers

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.)

like image 160
Stephen C Avatar answered Oct 17 '22 12:10

Stephen C


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 class Random 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 an int value and if the argument bits is between 1 and 32 (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 be 0 or 1.

...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...

like image 4
T.J. Crowder Avatar answered Oct 17 '22 11:10

T.J. Crowder