What is seed in util.Random?

2019-05-23 17:30发布

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.

标签: java random
3条回答
【Aperson】
2楼-- · 2019-05-23 18:09

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.

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.

查看更多
爷的心禁止访问
3楼-- · 2019-05-23 18:14

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(), then System.currentTimeMillis() will be used for the seed, which is good enough for almost all cases.

查看更多
倾城 Initia
4楼-- · 2019-05-23 18:23

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

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)

I strongly recommend to go through this answer:https://stackoverflow.com/a/23127798/9080948

and this video:https://youtu.be/86_cnhqSyh0

查看更多
登录 后发表回答