如何从 Java 中的 Random 中获取种子? [英] How do I get the seed from a Random in Java?

查看:24
本文介绍了如何从 Java 中的 Random 中获取种子?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为某个对象创建一个深度克隆.该对象包含一个 Random.

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

Random 中检索种子是一种好习惯吗?如果是这样,如何?没有 Random.getSeed().

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

推荐答案

A Random 旨在是随机的.通常你希望两个 Random 产生不同的数字而不是产生相同的数字.

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

您可以使用序列化/反序列化复制 Random 并使用反射获取种子"字段.(但我怀疑你是否应该这样做)

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

除非序列对您很重要,否则您可以认为 Random 的克隆是它本身或任何 new Random()

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

这篇关于如何从 Java 中的 Random 中获取种子?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆