aspose file tools*
The moose likes Beginning Java and the fly likes how to generate random numbers ? Random class always generate same number.... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "how to generate random numbers ? Random class always generate same number...." Watch "how to generate random numbers ? Random class always generate same number...." New topic
Author

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

raminaa niilian
Ranch Hand

Joined: Jul 14, 2005
Posts: 551
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
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

raminaa, can you show us more of your code?


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11402
    
  16

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?


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
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

Joined: Jan 30, 2000
Posts: 18671
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.


"I'm not back." - Bill Harding, Twister
sven studde
Ranch Hand

Joined: Sep 26, 2006
Posts: 148
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

Joined: Jan 30, 2000
Posts: 18671
[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

Joined: Sep 26, 2006
Posts: 148
Uhh, yeah. I read your post, and I saw how that was accomplished:

++seedUniquifier
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how to generate random numbers ? Random class always generate same number....