1

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.

Tanin
  • 1,853
  • 1
  • 15
  • 20
  • 2
    The answer has historically been "Yes" - if you use the same seed, java.util.Random will give you the same numbers: https://stackoverflow.com/a/12458415/421195. ALSO: look here: https://www.baeldung.com/java-generating-random-numbers – paulsm4 Sep 15 '22 at 19:49
  • By the way, Java 17 as new interface types and implementations for pseudorandom number generators (PRNGs). See [JEP 356](https://openjdk.org/jeps/356). – Basil Bourque Sep 15 '22 at 20:35
  • And FYI, Java 9 got more SecureRandom implementations. [JEP 273: DRBG-Based SecureRandom Implementations](https://openjdk.org/jeps/273) – Basil Bourque Sep 15 '22 at 20:53

3 Answers3

6

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.

Louis Wasserman
  • 191,574
  • 25
  • 345
  • 413
2

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.

PM 77-1
  • 12,933
  • 21
  • 68
  • 111
1

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.

Simulant
  • 19,190
  • 8
  • 63
  • 98