2

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?

Andy Thomas
  • 84,978
  • 11
  • 107
  • 151
SpringLearner
  • 13,738
  • 20
  • 78
  • 116

5 Answers5

10

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.

Carl Manaster
  • 39,912
  • 17
  • 102
  • 155
  • @SpringLearner `seed` is number based on which other results are generated. If you will not specify it it will be calculated most probably from current time. – Pshemo Jul 09 '15 at 16:12
  • @Pshemo No, `seed` simply means the initial state of the PRNG. Since a PRNG always generates the next number from the previous one, you have to provide a "zeroth number" before you generate the first. The metaphor is based on how a real seed can be considered the initial state of a plant. – biziclop Jul 09 '15 at 16:13
  • 1
    @biziclop But that is actually what I said: "number based on which other results are generated". To be more precise I could change "other results" to "next result" but to be fair, that *next result* wouldn't exist without previous result which at some point came from `seed` :) – Pshemo Jul 09 '15 at 16:21
  • @Pshemo You're right, I wrote my comment before you edited yours and it was less clear to me what you were trying to say. Sorry. – biziclop Jul 09 '15 at 16:34
  • @biziclop Yes, sorry for confusion. – Pshemo Jul 09 '15 at 16:46
  • to know what a seed is you need to know about [pseudorandom number generator](https://en.wikipedia.org/wiki/Pseudorandom_number_generator) – phuclv Jul 09 '15 at 17:29
7

A pseudorandom number generator works by repeatedly generating a new number based on the one it has previously generated. That means that if you always have the same first "random" number, and you use the same pseudorandom number generator to generate the second, you'll always have the same second "random" number as well.

The first Random constructor constructs a pseudorandom number generator with a nondeterminate seed (first number in the sequence), so you'll almost always end up with a different sequence of "random" numbers. The second Random constructor constructs a pseudorandom number generator with whatever seed you want, so if you give it the same seed, you'll always get the same sequence.

Here's an example. If you create a Random like this:

Random yourRandom = new Random();

it will start off with some seed. That seed could be 42, 121, 3810, whatever. You can never be sure when you create it. All the random numbers it generates are based off of that seed, and so since it nearly always uses a different seed, you'll nearly always get different "random" numbers out of it.

On the other hand, if you create a Random like this instead:

Random yourOtherRandom = new Random(36);

all the numbers yourOtherRandom generates will be calculated starting from 36. Since the first number (36) is the same, and the second number is calculated from the first, etc., everything yourOtherRandom generates will be the same every time you run your program.

Sam Estep
  • 12,974
  • 2
  • 37
  • 75
  • sorry but I did not understand your answer,please give an example – SpringLearner Jul 09 '15 at 16:14
  • you have passed 36 ,so what does it mean? Will the random number generated will always be greater than 36 or it will be always less than 36? – SpringLearner Jul 09 '15 at 16:20
  • @SpringLearner It doesn't give any guarantees about the generated numbers. It just means that all the other numbers will be calculated from 36 *in some way*. That calculation could be something as simple as adding 1 (unlikely), so your sequence could be 36, 37, 38, etc., or it could be something very complicated involving prime numbers and modulus (more likely), so your sequence could be something like 36, 120, 23, 43747, 1572, 18, 9, 5738, etc. The point is that every number is determined by the previous one. – Sam Estep Jul 09 '15 at 16:24
  • @SpringLearner Building from that example, if you start from 36, the next number would be 120, guaranteed. Or if you start from 43747, the next number would be 1572, guaranteed. The sequence is the same; all that matters is where you start. This isn't the same sequence as the one generated by `Random`, of course, but the same principle applies. – Sam Estep Jul 09 '15 at 16:24
  • My all doubts are still unclear,but some are cleared,+1 – SpringLearner Jul 09 '15 at 16:28
3

This is a fun one! The Random number generation is not random at all. If you use the same seed and ask it for a bunch of random numbers, you will get the same sequence. This is important as it allows different computers to predictably generate the same sequence as long as they have shared the seed. If you do not specify a seed, then one is chosen for you that is very unlikely to be chosen by any other VM in the world. BUT, if someone were to guess the seed you used, they would be able to generate the same sequence of numbers.

From Google search:Random search

Derek Hulley
  • 334
  • 1
  • 4
0

The Random generator is a Pseudorandom number generator. That means that it is, in fact, not a random number generator but only some clever algorithm that produces fully deterministic numbers that just look like random.

When the generator is used, each time it produces a random number it modifies its internal state in order to produce a different number at the next call. However, at the beginning the internal state of this algorithm must be initialised, and the value that is used for this initialisation is commonly called seed. The parameterless constructor makes a seed on its own, based on system time, while the other constructor lets you to put your seed, which allows you to make it repeatable - the same seed (and the same generator) will produce the same sequence of numbers.

If you are interested, here is the source code of the Random class from OpenJDK (i.e. the open source implementation of Java, but it should be functionally equivalent). The constructor with seed is at line 135, the setSeed method is at line 168 and e.g. the nextInt method is at line 328 which just calls a private method next which is at line 198 and which is where all the magic happens. It also has javadoc with the reference to the (probably more mathematical) description of this kind of generator.

zegkljan
  • 8,051
  • 5
  • 34
  • 49
-1

I wonder whether what you want to ask is about distinguishing the default constructor and argument constructor. When you instantiate a new Random object, you can code this:

Random random=new Random();

In this way, we invoke the default constructor, i.e no argument constructor. But if you code this:

Random random=new Random(47);

we invoke a constructor which argument is 47.

It is similar to C language; a seed will "randomly" create a number if you use the same seed and no matter when it will not change. But if you choose the first method, the number will modify once running!

Carl Manaster
  • 39,912
  • 17
  • 102
  • 155
shonminh
  • 158
  • 1
  • 2
  • 13