Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the use of the Random(long) constructor?

Tags:

java

random

There are 2 constructors of Random class

  1. public Random()
  2. public Random(long seed)

The description for the second constructor as per oracle states as

Creates a new random number generator using a single long seed. The seed is the initial value of the internal state of the pseudorandom number generator which is maintained by method next(int).

I did not understand it completely. And I did not find any articles/book which clearly explains why,when and how it is used.

Can any one explain please?

like image 553
SpringLearner Avatar asked Nov 28 '25 00:11

SpringLearner


1 Answers

If you use the constructor with the seed, you will get a repeatable sequence, so it's good for testing. If you use the constructor without the seed, you don't know what sequence of random-like numbers will be produced.

like image 56
Carl Manaster Avatar answered Nov 30 '25 15:11

Carl Manaster



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!