• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Rob Spoor
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Carey Brown
Bartenders:

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

 
Ranch Hand
Posts: 551
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
author
Posts: 23931
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
raminaa, can you show us more of your code?
 
lowercase baba
Posts: 13086
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 148
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Uhh, yeah. I read your post, and I saw how that was accomplished:

++seedUniquifier
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic