Re: best approach to generate random number in java

From: Tor Iver Wilhelmsen (tor.iver.wilhelmsen_at_broadpark.no)
Date: 10/13/04


Date: 13 Oct 2004 21:59:08 +0200

jrefactors@hotmail.com (Matt) writes:

> If I do it without a seed, it can pretty much generate different
> number from [0-9999] each time.
>
> Random generator = new Random();
> int r = Math.abs(generator.nextInt()) % 10000;

Check the source: This isn't without a seed, but with a "default" seed
equal to the value of System.currentTimeMillis().

> So what's the purpose of using a seed?

As others have pointed out, to recreate a given pseudorandom sequence.
Useful for encryption, storing randomly generated game maps as a long
etc.