How do I generate random integers within a specific range in Java?

Given two numbers Min and Max, the task is to generate a random integer within this specific range in Java.


Input: Min = 1, Max = 100
Output: 89

Input: Min = 100, Max = 899
Output: 514


  • Get the Min and Max which are the specified range.
  • Call the nextInt() method of ThreadLocalRandom class (java.util.concurrent.ThreadLocalRandom) and specify the Min and Max value as the parameter as
    ThreadLocalRandom.current().nextInt(min, max + 1);
  • Return the received random value




// Java program to generate a random integer
// within this specific range
import java.util.concurrent.ThreadLocalRandom;
class GFG {
    public static int getRandomValue(int Min, int Max)
        // Get and return the random integer
        // within Min and Max
        return ThreadLocalRandom
            .nextInt(Min, Max + 1);
    // Driver code
    public static void main(String[] args)
        int Min = 1, Max = 100;
        System.out.println("Random value between "
                           + Min + " and " + Max + ": "
                           + getRandomValue(Min, Max));



Random value between 1 and 100: 35

Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using or mail your article to See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Article Tags :
Practice Tags :

Be the First to upvote.

Please write to us at to report any issue with the above content.