Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the importance of using Random.setSeed?

Tags:

java

random

When writing Java program, we use setSeed in the Random class. Why would we use this method?

Can't we just use Random without using setSeed? What is the main purpose of using setSeed?

like image 302
nilashan Avatar asked Jul 10 '13 08:07

nilashan


2 Answers

One use of this is that it enables you to reproduce the results of your program in future.

As an example, I wanted to compute a random variable for each row in a database. I wanted the program to be reproducible, but I wanted randomness between rows. To do this, I set the random number seed to the primary key of each row. That way, when I ran the program again, I got the same results, but between rows, the random variable was pseudo random.

like image 194
Owen Avatar answered Oct 17 '22 19:10

Owen


The seed is used to initialize the random number generator. A seed is used to set the starting point for generating a series of random numbers. The seed sets the generator to a random starting point. A unique seed returns a unique random number sequence.

This might be of help .

A pseudorandom number generator (PRNG), also known as a deterministic random bit generator DRBG, is an algorithm for generating a sequence of numbers that approximates the properties of random numbers. The sequence is not truly random in that it is completely determined by a relatively small set of initial values, called the PRNG's state, which includes a truly random seed.

like image 27
AllTooSir Avatar answered Oct 17 '22 19:10

AllTooSir