java - util.Random 中的种子是什么?

标签 java random

我不明白Seed是什么意思在 java.util.Random ?我读过Why does this code print “hello world”?问题,我仍然对 seed 感到困惑。任何人都可以友好地向我描述一下 seed 的真正含义吗?谢谢。

setSeed() 的文档中方法 ... seed - the initial seed 是什么意思?

public void setSeed(long seed)
Sets the seed of this random number generator using a single long seed. The general contract of setSeed is that it alters the state of this random number generator object so as to be in exactly the same state as if it had just been created with the argument seed as a seed. The method setSeed is implemented by class Random by atomically updating the seed to
(seed ^ 0x5DEECE66DL) & ((1L << 48) - 1)
and clearing the haveNextNextGaussian flag used by nextGaussian().
The implementation of setSeed by class Random happens to use only 48 bits of the given seed. In general, however, an overriding method may use all 64 bits of the long argument as a seed value. Parameters:
seed - the initial seed

我希望如果我能准确理解 seed 的含义,我相信我会清楚地理解 this回答。

最佳答案

A pseudo-random number generator produces a sequence of numbers. It isn't truly random, but generally a mathematical calculation which produces an output that matches some desirable distribution, and without obvious patterns. In order to produce such a sequence, there must be state stored for the generator to be able to generate the next number in that sequence. The state is updated each time using some part of the output from the previous step.

Seeding explicitly initialises this state. A 'seed' is a starting point, from which something grows. In this case, a sequence of numbers.

This can be used either to always generate the same sequence (by using a known constant seed), which is useful for having deterministic behaviour. This is good for debugging, for some network applications, cryptography, etc.

Or, in situations where you want the behaviour to be unpredictable (always different each time you run a program, a card game perhaps), you can seed with a number likely to be continually changing, such as time.

The 'randomness' of the sequence does not depend on the seed chosen, though it does depend on not reseeding the sequence.

取自What is a seed in relation to a random number generation algorithm and why is computer time used to create this seed more often than not?

这应该可以回答您的问题。

关于java - util.Random 中的种子是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22530702/

相关文章:

java - 打乱数组列表中的元素组

java - 我应该在哪里存储图像(和其他文件)以供我的 Java 程序使用?

java - 空指针异常变通

java - ViewPager 未从服务器获取图像

java - 在 AS7 中转换查找的 EJB View 时出现 ClassCastException

c++ - 从数字和字母快速生成大量随机字符串

java - 在中央找不到 Artifact org.jboss.seam :jboss-seam:jar:2. 3.5.Final-redhat-1

linux - 在 Bash 中使用 $RANDOM 重复数字

powershell - Powershell Get-Random cmdlet 的默认种子是什么

c - Monty Hall 游戏 : How to print which door the computer opened