Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 259
  • Last Modified:

Generating Random Numbers in Java using the time as a seed.

Everything is working fine now, but I make it so that I have a different seed every time.

Right now I have:
 Random rand = new Random(2);

How do I modify this so that it uses the time as a seed?
Thanks,
Jonathan.
0
twibblej
Asked:
twibblej
  • 2
1 Solution
 
petmagdyCommented:
Try this:

Random rand = new Random();

each time get random number do thi:
Date date = new Date();
rand.setSeed(date.getTime());
int randomNumber = rand.nextInt();
0
 
hongjunCommented:
you do this
Random rand = new Random(System.currentTimeMillis());


hongjun
0
 
petmagdyCommented:
sorry little correction call setSeed() once
0
 
objectsCommented:
> How do I modify this so that it uses the time as a seed?

Random rand = new Random();

current time is the default seed
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now