Generating Random Numbers with a Specified Seed
suggest change//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. tlr.setSeed(12345L);
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()); ThreadLocalRandom.current().setSeed(System.currentTimeMillis());
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents