Getting random numbers in Java

In Java 1.7 or later, the standard way to do this is as follows:
import java.util.concurrent.ThreadLocalRandom;
int randomNum = ThreadLocalRandom.current().nextInt(min, max + 1);

min: minimum value

max: maximum value

Example
import java.util.concurrent.ThreadLocalRandom; public class TestClass { public static void main(String[] args) { System.out.println(ThreadLocalRandom.current().nextLong(50, 100)); } }

Above code return a long value between 50 (inclusive) and 101 (exclusive)

Before Java 1.7 , the standard way to do this is as follows:
import java.util.Random; Random rand; int randomNum = rand.nextInt((max - min) + 1) + min;
Example
import java.util.Random; public class TestClass { public static void main(String[] args) { Random rand = new Random(); System.out.println(rand.nextInt(100) + 1); } }

Above code return the value between 1 minimum and 100 is the maximum