I can't understand what was the meaning of Seed in java.util.Random ? I had read Why does this code print “hello world”? question and I am still confuse about seed . Can anyone describe me kindfully what was seed actually mean ? Thanks.
In documentation for setSeed() method ... what does mean 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
I would expect if I can understand exactly meaning of seed
, I am sure I will understand clearly to this answer.
Taken from 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?
This should answer your question.
The pseudorandom number generator is implemented in terms of an integer which is, each time you ask for a number, transformed into another integer by the pseudorandom sequence generator function.
The initial value of that internal integer is termed the seed. The idea is to set it differently each time you instantiate
Random
because the pseudorandom sequence is fully deterministic once the seed is assigned.If you use the nullary constructor,
new Random()
, thenSystem.currentTimeMillis()
will be used for the seed, which is good enough for almost all cases.java.util.Random.setSeed(long seed): Sets the seed of this random number generator using a single long seed
Syntax: public void setSeed(long seed)
Parameters: seed - the initial seed
I strongly recommend to go through this answer:https://stackoverflow.com/a/23127798/9080948
and this video:https://youtu.be/86_cnhqSyh0