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.
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 classRandom
. Java implementations must use all the algorithms shown here for theclass Random
, for the sake of absolute portability of Java code. However, subclasses ofclass Random
are permitted to use other algorithms, so long as they adhere to the general contracts for all the methods.
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