Search code examples
javarandomclonerandom-seed

How do I get the seed from a Random in Java?


I am creating a deep clone for some object. The object contains a Random.

Is it good practice to retrieve the seed from the Random? If so, how? There isn't a Random.getSeed().


Solution

  • A Random is intended to be random. Usually you want two Random to produce different numbers rather than to produce the same numbers.

    You can copy a Random using serialisation/de-serialisation and get the "seed" field using reflection. (But I doubt you should be doing either)

    Unless the sequence is critical to you, you can take the view that the clone of a Random is itself or any new Random()