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
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.
- Random vs Secure Random numbers in Java
- Generate random String of given size in Java
- Generate Infinite Stream of Integers in Java
- Creating a Cell at specific position in Excel file using Java
- Remove an Element at specific index from an Array in Java
- Replace a character at a specific index in a String in Java
- Java.util.Random class in Java
- Java.util.Random.nextInt() in Java
- Method within method in java
- Open Specific Settings Using Android Application
- Generating random numbers in Java
- Image Processing in Java | Set 7 (Creating a random pixel image)
- Java Math random() method with Examples
- A Java Random and StringBuffer Puzzle
- StrictMath random() Method in Java
- Random nextBoolean() method in Java with Examples
- Random nextFloat() method in Java with Examples
- Random nextDouble() method in Java with Examples
- Random nextGaussian() method in Java with Examples
- Random next() method in Java with Examples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. 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.