how to generate random numbers ? Random class always generate same number....
raminaa niilian
Ranch Hand
Joined: Jul 14, 2005
Posts: 550
posted
0
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.
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.
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?
Never ascribe to malice that which can be adequately explained by stupidity.
Michael Dunn
Ranch Hand
Joined: Jun 09, 2003
Posts: 4632
posted
0
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: 18670
posted
0
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
posted
0
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: 18670
posted
0
[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
posted
0
Uhh, yeah. I read your post, and I saw how that was accomplished: