Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reproducibility of java pseudo-random numbers across systems and versions?

I need to generate a controlled sequence of pseudo-random numbers, given an initial integer parameter. For that I'm using the standard Java Random class, seeded by an integer parameter. I'd like to make sure that I will generate the same sequence across systems (Operating system, but also Java/JDK version), in the foreseeable future (and more!).

In summary: Does Java ensure the reproducibility / portability of it's pseudo-random number generator across implementation and versions?

Note: I've asked the exact same question for Python. I since changed the implementation language to Java but for other reasons.

like image 402
Laurent Grégoire Avatar asked Feb 05 '12 18:02

Laurent Grégoire


1 Answers

Yes.

The javadoc explains:

An instance of this class is used to generate a stream of pseudorandom numbers. The class uses a 48-bit seed, which is modified using a linear congruential formula. (See Donald Knuth, The Art of Computer Programming, Volume 2, Section 3.2.1.)

If two instances of Random are created with the same seed, and the same sequence of method calls is made for each, they will generate and return identical sequences of numbers. In order to guarantee this property, particular algorithms are specified for the class Random. Java implementations must use all the algorithms shown here for the class Random, for the sake of absolute portability of Java code. However, subclasses of class Random are permitted to use other algorithms, so long as they adhere to the general contracts for all the methods.

like image 50
Mike Samuel Avatar answered Oct 22 '22 00:10

Mike Samuel