Home > front end >  Is java.util.Random deterministic given the same seed and JVM version?
Is java.util.Random deterministic given the same seed and JVM version?

Time:09-16

I'm investigating the elusive Spark's indeterminacy exception, and I suspect that java.util.Random is not always deterministic.

I wonder if anyone knows if java.util.Random is deterministic assuming the seed and JVM version are identical.

Thank you.

CodePudding user response:

java.util.Random specifies the exact algorithm it uses to generate random numbers. As a result, it is deterministic assuming the same seed -- whether or not the JVM version is identical.

CodePudding user response:

From the docs:

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.

CodePudding user response:

Yes, if the seeds are the same, the result will also be the same.

If you don't trust the documentation, trust the source. It is publicly available:

https://hg.openjdk.java.net/jdk8/jdk8/jdk/file/tip/src/share/classes/java/util/Random.java

The output is only based on the seed (input) and the algorithm to calculate the next number based on the current seed, which will be updated as well. No other "unvisible", "random" input is used.

  •  Tags:  
  • java
  • Related