简体   繁体   English

Java Random对象可以在同一种子的不同执行中给出不同的结果吗?

[英]Can a Java Random object give different results in different executions for the same seed?

Suppose you have a Java code in which you are using a Random object, initialized with a constant seed. 假设您有一个Java代码,您在其中使用Random对象,使用常量种子进行初始化。 In your code you use the Random object to generate different pseudorandom integers. 在您的代码中,您使用Random对象生成不同的伪随机整数。 You are not using multithreading. 您没有使用多线程。 Is it possible to have different results in different executions of the code? 在不同的代码执行中是否可能有不同的结果? For example, in case that the Java compiler deletes any redundant calls of the Random object in some random way in order to optimize the code, the execution will be different each time if you recompile the code. 例如,如果Java编译器以某种随机方式删除Random对象的任何冗余调用以优化代码,则每次重新编译代码时执行都会有所不同。

Theoretically a different JVM can use a different implementation for the pseudo random algorithm. 从理论上讲,不同的JVM可以对伪随机算法使用不同的实现。 Different JVM versions could also do that, since it's not specified anywhere that it needs to return the same values for a given seed between different versions. 不同的JVM版本也可以这样做,因为它不需要在任何地方指定它需要为不同版本之间的给定种子返回相同的值。 However as far as I know the Random implementation in Oracle's JDK has been the same since its inception. 但据我所知,Oracle JDK中的Random实现自成立以来就一直如此。

Given the same execution environment the output is guaranteed. 在相同的执行环境下,输出得到保证。

No, this is pretty much guaranteed , assuming the same environement between two runs. 不,这是非常有保证的 ,假设两次运行之间有相同的环境。

A PRNG can be started from an arbitrary initial state using a seed state. 可以使用种子状态从任意初始状态开始PRNG。 It will always produce the same sequence when initialized with that state 当用该状态初始化时,它将始终产生相同的序列

And thank god the optimizer will never change anything with direct consequences on side-effects. 感谢上帝,优化器永远不会改变任何东西,直接影响副作用。

If it removes dead code, it's because the code is deemed dead on each possible state of the program, so no possible impact on the call sequence to Random . 如果它删除了死代码,那是因为代码在程序的每个可能状态下都被认为是死的,所以对Random的调用序列没有可能的影响。

I think it should be consistent given you use the same JVM on the same system. 我认为,鉴于您在同一系统上使用相同的JVM,它应该是一致的。 I just tried the following. 我刚刚尝试了以下内容。

import java.util.Random;

public class test
{
    public static void main(String[] args)
    {
        Random r = new Random(123);
        System.out.println(r.nextInt());
    }
}

It gave the same output every time I run it on my machine. 每次在我的机器上运行时,它都会输出相同的输出。 I am using Open JDK 1.8.0_91 on Ubuntu 16.04. 我在Ubuntu 16.04上使用Open JDK 1.8.0_91。 The value I get when I run this code is -1188957731. 运行此代码时获得的值是-1188957731。 Why don't you try to run it and see if you get the same value. 你为什么不尝试运行它,看看你是否得到相同的价值。

Unless you introduce any other source of entropy (such as use input or some way the environment can affect the program), the same random seed will produce the same result on every execution. 除非您引入任何其他熵源(例如使用输入或环境可能影响程序的某种方式),否则相同的随机种子将在每次执行时产生相同的结果。

Note that this relies to the execution of the same byte code. 请注意,这依赖于执行相同的字节代码。 Theoretically, changing your compiler to compile the same source code could produce a different byte code that would produce a different output, but it would be pretty difficult to find a real example to demonstrate this. 从理论上讲,更改编译器以编译相同的源代码可能会产生不同的字节代码,从而产生不同的输出,但要找到一个真实的例子来证明这一点很难。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM