跨系统和版本的Java伪随机数的可重现性? [英] Reproducibility of java pseudo-random numbers across systems and versions?

查看:143
本文介绍了跨系统和版本的Java伪随机数的可重现性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给定一个初始整数参数,我需要生成一个受控的伪随机数序列.为此,我使用标准的Java随机类,该类由整数参数设定.我想确保在可预见的将来(甚至更多),我将在系统(操作系统,还有Java/JDK版本)中生成相同的序列.

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

总结:Java是否可以确保其实现版本之间的伪随机数生成器的可重复性/可移植性?

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

注意:我已经问过完全一样Python问题.此后,由于其他原因,我将实现语言更改为Java.

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

推荐答案

是.

javadoc 解释:

The javadoc explains:

此类的一个实例用于生成伪随机数流.该类使用48位种子,可以使用线性同余公式对其进行修改. (请参见Donald Knuth,《计算机编程艺术》,第2卷,第3.2.1节)

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

如果使用相同的种子创建Random的两个实例,并且对每个实例执行相同的方法调用序列,它们将生成并返回相同的数字序列.此属性为Random 类指定了特定算法.为了实现Java代码的绝对可移植性,Java实现必须使用此处显示的所有class Random算法. 但是,class Random的子类允许使用其他算法,只要它们遵守所有方法的通用协定即可.

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.

这篇关于跨系统和版本的Java伪随机数的可重现性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆