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

后端 未结 7 1474
北荒
北荒 2020-12-03 09:46

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,

7条回答
  •  余生分开走
    2020-12-03 10:42

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

提交回复
热议问题