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

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

问题描述

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

I am creating a deep clone for some object. The object contains a 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来产生不同的数字,而不是产生相同的数字。

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)

除非序列对你很重要,你可以认为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天全站免登陆