How to generate Random number in Java

By | October 4, 2015

If you want to generate random number in Java don’t use :

 Random rd = new Random() 

It was proved that this class does not generate really random number. The solution is to use the ThreadLocalRandom class.
This is an example :

ThreadLocalRandom random = ThreadLocalRandom.current();

// create a number between 0 and 2

intNextInt = random.nextInt(3);


You can also use it to querying for  a number in an interval like this :

intNextInt = random.nextInt(2,5);


See you soon.


Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.