Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how to generate random numbers ? Random class always generate same number....

 
raminaa niilian
Ranch Hand
Posts: 551
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
Thank you for reading my post
i need to generate some random numbers in my java program
I tried to use

but it always generate the same number , it never give me a new number .
for example i need to generate 80 random number but it return 80 same number in its 80 call.
the number just change once after i close and execute application again.

thanks
 
Henry Wong
author
Marshal
Pie
Posts: 21129
78
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by raminaa niilian:
Hi
Thank you for reading my post
i need to generate some random numbers in my java program
I tried to use

but it always generate the same number , it never give me a new number .
for example i need to generate 80 random number but it return 80 same number in its 80 call.
the number just change once after i close and execute application again.

thanks


Really?? I just did this...



And it printed out 80 different numbers.

Henry
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
raminaa, can you show us more of your code?
 
fred rosenberger
lowercase baba
Bartender
Posts: 12127
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
are you running the program over and over and getting the same values, or are you saying that withing a single run, you never get a different number?

i'm rusty, but don't you have to provide a seed value somewhere to get different values each time through?
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
doing it this way reproduces your problem



so, only create rnd once, per Henry's example, and you should be OK
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Note that this behavior should only occur using older JDKs, version 1.4 or earlier. As of JDK 1.5 the code has changed to make it much more likely that each new Random() instance will get a new and different seed, when using the no-arg constructor.

But regardless of JDK version, it makes sense to do as Michael said: just create a single Random instance (outside the loop), and keep reusing that, rather than creating a new one each time. Then this problem will go away.
 
sven studde
Ranch Hand
Posts: 148
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i'm rusty, but don't you have to provide a seed value somewhere to get different values each time through?

If you seed the Random number generator with the same seed every time, you will get the same sequence of numbers:

Random sequence = new Random(100L);

However, the default constructor for Random uses the current system time for the seed:

Random sequence = new Random();

which should produce a unique seed unless you create two Random objects in a row and the code executes so fast that the system time hasn't changed.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[sven]: which should produce a unique seed unless you create two Random objects in a row and the code executes so fast that the system time hasn't changed.

Unless, of course, you're using JDK 1.5+, in which case it will produce a unique seed each time. As just discussed.
 
sven studde
Ranch Hand
Posts: 148
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Uhh, yeah. I read your post, and I saw how that was accomplished:

++seedUniquifier
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic