javaandroid-studiorandomsrand

Unique random - srand in java ? (like in c++)


Currently I'm using a

Random rand = new Random();
myNumber = rand.nextInt((100-0)+1)+0;

and it's a pseudo-random, because I always get the same sequence of numbers. I remember in c++ you could just use the ctime and use srand to make it unique. How do you do it in java ?


Solution

  • For the best randomness in every solution, it is important to RE-USE the random instance. If this isn't done, most of the output it gives will be similar over the same timespan, and at the case of a thrown away SecureRandom, it will spend a lot of time recreating the new random.