Reproducibility of java pseudo-random numbers acro

2020-03-01 06:52发布

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.

1条回答
淡お忘
2楼-- · 2020-03-01 07:38

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.

查看更多
登录 后发表回答