Java 1.7 or Higher Versions
import java.util.concurrent.ThreadLocalRandom;
public class TestJava {
public static void main(String[] args) {
int min = 100;
int max = 200;
// nextInt is normally exclusive of the top value,
// so add 1 to make it inclusive
int randomNumber = ThreadLocalRandom.current().nextInt(min, max + 1);
System.out.println(randomNumber);
}
} |
import java.util.concurrent.ThreadLocalRandom;
public class TestJava {
public static void main(String[] args) {
int min = 100;
int max = 200;
// nextInt is normally exclusive of the top value,
// so add 1 to make it inclusive
int randomNumber = ThreadLocalRandom.current().nextInt(min, max + 1);
System.out.println(randomNumber);
}
}
Before Java 1.7
import java.util.Random;
public class TestJava {
public static void main(String[] args) {
int min = 100;
int max = 200;
Random random = new Random();
// nextInt is normally exclusive of the top value,
// so add 1 to make it inclusive
int randomNumber = random.nextInt((max - min) + 1) + min;
System.out.println(randomNumber);
}
} |
import java.util.Random;
public class TestJava {
public static void main(String[] args) {
int min = 100;
int max = 200;
Random random = new Random();
// nextInt is normally exclusive of the top value,
// so add 1 to make it inclusive
int randomNumber = random.nextInt((max - min) + 1) + min;
System.out.println(randomNumber);
}
}