What is seed in util.Random?
我不明白seed在java.util.random中的含义是什么?我读过为什么这个代码印着"你好,世界"?问题是,我仍然对种子感到困惑。有人能亲切地描述一下Seed是什么意思吗?谢谢。
在setseed()方法的文档中…
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
如果我能准确理解
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.
从一个种子与随机数生成算法的关系中得出,为什么用计算机时间来创建这个种子的频率更高?
这应该能回答你的问题。
伪随机数生成器是根据一个整数实现的,即每次请求一个数字时,都会通过伪随机序列生成器函数转换为另一个整数。
内部整数的初始值称为种子。其思想是每次实例化
如果使用空构造函数
Java.UTI.Actudio .StStand(长种子):使用单个长种子设置这个随机数生成器的种子
Syntax:公共空隙设置种子(长种子)
参数:种子-初始种子
Every Random constructed with the same seed will generate the same
pattern of numbers every time.So basically we set seed with a long value when we want to get the
same random number sequence every time (like in video
games,debugging,etc)
我强烈建议通过以下答案:https://stackoverflow.com/a/23127798/9080948
还有这段视频:https://youtu.be/86_cnhqsyh0