Java Language Random Number Generation Generating Random Numbers with a Specified Seed

30% OFF - 9th Anniversary discount on Entity Framework Extensions until December 15 with code: ZZZANNIVERSARY9


//Creates a Random instance with a seed of 12345.
Random random = new Random(12345L);

//Gets a ThreadLocalRandom instance
ThreadLocalRandom tlr = ThreadLocalRandom.current();

//Set the instance's seed.

Using the same seed to generate random numbers will return the same numbers every time, so setting a different seed for every Random instance is a good idea if you don't want to end up with duplicate numbers.

A good method to get a Long that is different for every call is System.currentTimeMillis():

Random random = new Random(System.currentTimeMillis());

Got any Java Language Question?