Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is seed in util.Random?

Tags:

java

random

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.

like image 790
Cataclysm Avatar asked Nov 28 '22 07:11

Cataclysm


2 Answers

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.

like image 185
anirudh Avatar answered Dec 04 '22 00:12

anirudh


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.

like image 41
Marko Topolnik Avatar answered Dec 03 '22 23:12

Marko Topolnik