Finding random Long number with range in Java

If we have to find a random integer we can use Random() api like this :

new Random().nextInt();

If we know the upper bound, we can specify it is one of the parameters like this

new Random().nextInt(1000);

So this code will generate any random number between 0 and 1000 excluding 1000.

As there is a method for random integer generation we would have expected a similar method for Long also. Something like this :

new Random().nextLong(100000);

Unfortunately it does not exist but fortunately Java7 has a new api for this

ThreadLocalRandom.current().nextLong(100000);

Mar Java Mit Java 🙂

Uday Ogra

Connect with me at http://facebook.com/tendulkarogra and lets have some healthy discussion :)

You may also like...

Leave a Reply

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