What is a seed parameter in method Random? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

What is a seed parameter in method Random?

I encountered an instance of using a parameter called seed in defining a random object in java. It goes like this: Random rand= new Random(47); 47 is the seed. I was wondering what does the seed do?

10th Jun 2017, 2:57 AM
Vahid Shirbisheh
Vahid Shirbisheh - avatar
3 Answers
+ 8
Random numbers are generated with an algorithm, and the seed determines what sequence it puts out. If you keep using the same seed, the algorithm will produce the same set of numbers every time you run the program. Random() on its own (without a given seed) will generate one for itself using the comps. clock.
10th Jun 2017, 3:50 AM
Tamra
Tamra - avatar
+ 10
Sometimes it may be useful for testing, to make sure calculations are working correctly (you'll know what's going in every time and expect a certain result) or if an issue is replicatable under the same circumstances. Anyway, yw :D
10th Jun 2017, 5:10 AM
Tamra
Tamra - avatar
+ 3
So why someone wants to use the same sequence of randoms over and over. Somehow it conflicts with the essence of randomness. Anyway thanks for your answer.
10th Jun 2017, 4:05 AM
Vahid Shirbisheh
Vahid Shirbisheh - avatar