• Post Reply Bookmark Topic Watch Topic
  • New Topic

Random class  RSS feed

 
Puspender Tanwar
Ranch Hand
Posts: 471
2
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Random constructor
Random constructor : public Random(long seed)
what is meant by seed?


this code will call the above constructor(correct me if I am wrong), how the value is printed. I tried but the output was confusing for me, nextInt(5) should print numbers between 0(include) to 5(exclude) randomly on each run, but it is printing the same value at every run , even I am unable to understand that how that particulat integer is coming ?
 
Mike. J. Thompson
Bartender
Posts: 689
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The random class is not truly random. It has no ability to generate randomness. It is what is called pseudo-random. Basically a truly random source is used to generate a large list of random numbers, and the Random class ships with that list.

The list never changes, so each time you use the Random class it is iterating over the same numbers. If that was all it did you would always get the same numbers in the same order. To stop that happening the Random class uses the seed. It combines the seed with the numbers in order to change the numbers.

But the mathematical operation it performs is still deterministic, so if you use the same seed you will get the same numbers back each time.

If you want different numbers each time you need to use a different seed each time. One strategy would be to use the current time in milliseconds (the System class has a method to return that).

The Random class also has a no-argument constructor that will provide a suitable seed for you.
 
Campbell Ritchie
Marshal
Posts: 55772
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
… but as MJT has said, if you use
new Random(1234567890987654321L)
you will always get the same sequence of numbers from it.
 
Puspender Tanwar
Ranch Hand
Posts: 471
2
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mike. J. Thompson wrote:
The list never changes, so each time you use the Random class it is iterating over the same numbers. If that was all it did you would always get the same numbers in the same order. To stop that happening the Random class uses the seed. It combines the seed with the numbers in order to change the numbers.

But the mathematical operation it performs is still deterministic, so if you use the same seed you will get the same numbers back each time.

The Random class also has a no-argument constructor that will provide a suitable seed for you.

hello mike, can you please explain these using some code, I am not getting anything

Campbell Ritchie wrote:… but as MJT has said, if you use
new Random(1234567890987654321L)
you will always get the same sequence of numbers from it. .


output : -1413978596 (each time of run)


output: 1 (each time of run)

what does it actually means :you will always get the same sequence of numbers from it.
 
Winston Gutkowski
Bartender
Posts: 10573
65
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Puspender Tanwar wrote:what does it actually means :you will always get the same sequence of numbers from it.

Exactly what you're seeing.

If, instead of calling rd.nextInt(2) - or indeed rd.nextInt(100), which would make it more obvious - once, you called it three times, you would get the same three numbers, in the same order.

Winston
 
Puspender Tanwar
Ranch Hand
Posts: 471
2
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
truthfully , still I am confused, what this seed is for ?
and why it always give the same number ?
Campbell Ritchie and Milke , may be you have explained all this in your post, but I didn't understood the point
 
Henry Wong
author
Sheriff
Posts: 23283
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Puspender Tanwar wrote: truthfully , still I am confused, what this seed is for ?


The mathematical formula that generates a stream of pseudo random numbers takes an input (ie. the seed). If you give it the same input, it will always generate the same series of outputs. So...

Puspender Tanwar wrote:
and why it always give the same number ?


You should never give it the same number, unless you want it to have the same series of "random" numbers. Also, the class has a constructor that doesn't take a seed. If you use that constructor, the class will use a combination of an incrementing variable and the nano timer. If you want random numbers, and you don't think that the default seed is good enough, I highly doubt that a constant is better.

Henry
 
Winston Gutkowski
Bartender
Posts: 10573
65
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Puspender Tanwar wrote: truthfully , still I am confused, what this seed is for ?

Ah, now that's a slightly different question.

Let's say that you've designed a card game and wanted to test it. Card games usually rely on the order of the cards being random, but many of the more complex games (eg, Bridge) have logic that relies on a series of events that take place after that "random deal" takes place (bidding, trick play etc).

So: in such a situation, wouldn't it be nice to "recreate" a deal, and run through it step-by-step to make sure that your logic is, in fact, working the way it should?

And THAT'S one of the great things about having a seed. The same seed produces the same set of "random" numbers in the same order, so you can use a particular seed to recreate all the numbers that produced an earlier "deal".

And just to re-iterate: A pseudo-random number generator (as pretty much every RNG in computing is) is NOT random. It simply returns a set of numbers where:
(a) The probability of any particular number appearing is no greater than any other.
(b) Given a number n from the generator, it is, to all intents and purposes, impossible to "predict" what the NEXT number will be, even statstically - ie, you can't "reverse-engineer" the algorithm.

HIH

Winston
 
Puspender Tanwar
Ranch Hand
Posts: 471
2
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you Winston Gutkowski and Henry
now it is little understood to me, but tell me one more thing, while using seeds, can we predict what pseudo random number will be thrown (as it always give the same value) ??
 
Winston Gutkowski
Bartender
Posts: 10573
65
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Puspender Tanwar wrote:thank you Winston Gutkowski and Henry
now it is little understood to me, but tell me one more thing, while using seeds, can we predict what pseudo random number will be thrown (as it always give the same value) ??

If you're using the same seed as a previous run: yes. If not: no.

Therefore, when you're testing, you may well want to use a specific seed; but in real life you'll want to use something like nanoSecond() (as suggested above) or simply use
  new Random();

Winston
 
Puspender Tanwar
Ranch Hand
Posts: 471
2
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:
If you're using the same seed as a previous run: yes.

how ??
 
Mike. J. Thompson
Bartender
Posts: 689
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Puspender Tanwar wrote:
Winston Gutkowski wrote:
If you're using the same seed as a previous run: yes.

how ??


A Random instance that is initiated with a specific seed value will always produce exactly the same list of numbers. Imagine that you write a program to print 1000 random numbers using a seed of 12345, and you write those 1000 numbers down. If you come back tomorrow and run the program again you will get exactly the same list of numbers, and exactly the same list the next day ad inifinitum.

And as an update to my earlier posts, I was slightly incorrect with my description. It seems the Random class uses only the seed to produce a list of pseudorandom numbers using a linearly congruential formula. If you reverse-engineered the class and worked out what this formula was then you could predict what sequence of numbers a Random instance would produce without having to create it, but then you may as well have just created the Random instance and used that.

See the Javadoc for Random here.
 
Puspender Tanwar
Ranch Hand
Posts: 471
2
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mike. J. Thompson wrote: Imagine that you write a program to print 1000 random numbers using a seed of 12345, and you write those 1000 numbers down. If you come back tomorrow and run the program again you will get exactly the same list of numbers, and exactly the same list the next day ad inifinitum.

this was also a question arose into my head. Now its clear . Thank you Mike
 
Campbell Ritchie
Marshal
Posts: 55772
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mike. J. Thompson wrote: . . . If you reverse-engineered the class and worked out what this formula was . . .
You don't have to. The documentation tells you what the formula is. You can also find the src.zip file in your Java® installation folder and unzip that and read the source code. Since the Random class' methods are not marked native, you should find everything there.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!