Win a copy of Terraform in Action this week in the Cloud forum!
  • 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:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

Random numbers

 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,
I�ve this easy question: how do i get random numbers? I�ve seen the Random class, but it says that "If two instances of Random are created with the same seed, and the same sequence of method calls is made for each, they will generate and return identical sequences of numbers". My problem is that i have a thread pool, and the code each thread executes must connect to another program, so i decided to use random numbers as usernames, but i�ve i use the Random class, as it will be executed always the same it will generate always the same number, no??
This is how i did it before i realized this happened, and as this code will be executed by each thread in the same order it will generate the same user variable:

String user = "user";
Random rand_user = new Random();
user = user+rand_user.nextInt();
connection.connect(null, 0, user, false, true);

How can i do in order to get different random numbers?
Thanks in advance!!
 
author and iconoclast
Posts: 24203
44
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The operative phrase is with the same seed. The seed is used to initialize the random number calculations. Two generators with two different seeds will give different sequences of numbers. So if you use a different seed in each thread, you'll be all set.

If you construct a Random with no constructor arguments, the current time in milliseconds is used as a seed; if all your Threads are created at once, chances are you'll get duplicate seeds.
 
Gus Spain
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok, i create all the threads at the same time when i create the thread pool, but they are waiting until clients arrive, and as they start waking up they start executing the code which includes the random function. As i see it, although they are create it at the same time, they execute the random in different times, so the seed in miliseconds should be different, shouldn�t they? If not, how could i fix it?
thanks!
 
Ranch Hand
Posts: 245
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can always reseed the generator. Pass it System.currentTimeMillis() at the time that your thread is woken up.
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic