You could use e.g. r.nextInt(101)

For a more generic "in between two numbers" use:

Random r = new Random();
int low = 10;
int high = 100;
int result = r.nextInt(high-low) + low;

This gives you a random number in between 10 (inclusive) and 100 (exclusive)

Answer from Erik on Stack Overflow
๐ŸŒ
Quora
quora.com โ€บ How-can-random-numbers-in-a-range-be-generated-in-Java
How can random numbers in a range be generated in Java? - Quora
Math.random() returns a values between 0 and 1. It returns a double, but it will always be less than 1. Then we multiply that result by our range. So if our min is 3 and our max is 5, our range will be 3. With that, for a random value of .25, ...
Top answer
1 of 16
4401

Java 7+

In Java 1.7 or later, the standard way to do this (generate a basic non-cryptographically secure random integer in the range [min, max]) is as follows:

import java.util.concurrent.ThreadLocalRandom;

// nextInt is normally exclusive of the top value,
// so add 1 to make it inclusive
int randomNum = ThreadLocalRandom.current().nextInt(min, max + 1);

See the relevant JavaDoc. This approach has the advantage of not needing to explicitly initialize a java.util.Random instance, which can be a source of confusion and error if used inappropriately.

However, conversely with ThreadLocalRandom there is no way to explicitly set the seed so it can be difficult to reproduce results in situations where that is useful such as testing or saving game states or similar.

Java 17+

As of Java 17, the psuedorandom number generating classes in the standard library implement the RandomGenerator interface. See the linked JavaDoc for more information. For example, if a cryptographically strong random number generator is desired, the SecureRandom class can be used.

Earlier Java

Before Java 1.7, the standard way to do this is as follows:

import java.util.Random;

/**
 * Returns a pseudo-random number between min and max, inclusive.
 * The difference between min and max can be at most
 * <code>Integer.MAX_VALUE - 1</code>.
 *
 * @param min Minimum value
 * @param max Maximum value.  Must be greater than min.
 * @return Integer between min and max, inclusive.
 * @see java.util.Random#nextInt(int)
 */
public static int randInt(int min, int max) {

    // NOTE: This will (intentionally) not run as written so that folks
    // copy-pasting have to think about how to initialize their
    // Random instance.  Initialization of the Random instance is outside
    // the main scope of the question, but some decent options are to have
    // a field that is initialized once and then re-used as needed or to
    // use ThreadLocalRandom (if using at least Java 1.7).
    // 
    // In particular, do NOT do 'Random rand = new Random()' here or you
    // will get not very good / not very random results.
    Random rand;

    // nextInt is normally exclusive of the top value,
    // so add 1 to make it inclusive
    int randomNum = rand.nextInt((max - min) + 1) + min;

    return randomNum;
}

See the relevant JavaDoc. In practice, the java.util.Random class is often preferable to java.lang.Math.random().

In particular, there is no need to reinvent the random integer generation wheel when there is a straightforward API within the standard library to accomplish the task.

2 of 16
1520

Note that this approach is more biased and less efficient than a nextInt approach, https://stackoverflow.com/a/738651/360211

One standard pattern for accomplishing this is:

Min + (int)(Math.random() * ((Max - Min) + 1))

The Java Math library function Math.random() generates a double value in the range [0,1). Notice this range does not include the 1.

In order to get a specific range of values first, you need to multiply by the magnitude of the range of values you want covered.

Math.random() * ( Max - Min )

This returns a value in the range [0,Max-Min), where 'Max-Min' is not included.

For example, if you want [5,10), you need to cover five integer values so you use

Math.random() * 5

This would return a value in the range [0,5), where 5 is not included.

Now you need to shift this range up to the range that you are targeting. You do this by adding the Min value.

Min + (Math.random() * (Max - Min))

You now will get a value in the range [Min,Max). Following our example, that means [5,10):

5 + (Math.random() * (10 - 5))

But, this still doesn't include Max and you are getting a double value. In order to get the Max value included, you need to add 1 to your range parameter (Max - Min) and then truncate the decimal part by casting to an int. This is accomplished via:

Min + (int)(Math.random() * ((Max - Min) + 1))

And there you have it. A random integer value in the range [Min,Max], or per the example [5,10]:

5 + (int)(Math.random() * ((10 - 5) + 1))
๐ŸŒ
Blogger
javarevisited.blogspot.com โ€บ 2013 โ€บ 05 โ€บ how-to-generate-random-numbers-in-java-between-range.html
How to Generate Random Numbers in Java Between Range - Example Tutorial
August 3, 2021 - You can also use Math.random() method to first create random number as double and than scale that number into int later. So you can create random integers in two step process. ... Similar to random integers in Java, java.util.Random class provides method nextDouble() which can return uniformly ...
๐ŸŒ
Vultr Docs
docs.vultr.com โ€บ java โ€บ standard-library โ€บ java โ€บ lang โ€บ Math โ€บ random
Java Math random() - Generate Random Number | Vultr Docs
September 27, 2024 - The Math.random() method in Java serves as a powerful and easy-to-implement tool for generating pseudo-random numbers. Whether you need a simpleset of random numbers, integers within a defined range, or simulate ...
๐ŸŒ
Java67
java67.com โ€บ 2018 โ€บ 01 โ€บ 3-ways-to-generate-random-integers-on.html
3 ways to create random numbers in a range in Java - Examples | Java67
4) The best way to generate random integers between a range is by using the RandomUtils class from Apache Commons Lang. This was added to a newer version of Apache commons-lang and returns an integer value between two given numbers.
๐ŸŒ
Baeldung
baeldung.com โ€บ home โ€บ java โ€บ java numbers โ€บ generating random numbers in a range in java
Generating Random Numbers in a Range in Java | Baeldung
May 11, 2024 - ... Generate Bounded and Unbounded Random Strings using plain Java and the Apache Commons Lang library. ... Math.random gives a random double value that is greater than or equal to 0.0 and less than 1.0.
๐ŸŒ
W3Docs
w3docs.com โ€บ java
Java Generate Random Number Between Two Given Values
import java.util.Random; public ... between 10 (inclusive) and 20 (exclusive). You can also use the nextDouble method to generate a random double between two values....
Find elsewhere
๐ŸŒ
Educative
educative.io โ€บ answers โ€บ how-to-generate-random-numbers-in-java
How to generate random numbers in Java
Math.random() produces a random double in the interval from 0 (inclusive) to 1 (exclusive). Ideal for straightforward random number generation and scaling to other ranges.
๐ŸŒ
Mkyong
mkyong.com โ€บ home โ€บ java โ€บ java โ€“ generate random integers in a range
Java - Generate random integers in a range - Mkyong.com
August 19, 2015 - This code is the easiest way to return 10 random numbers between 1 and 99. I took all your ideas and came up with this brief but effective code. Just change the values of 99,1,1 to your min and max to get your #s. If you use 99 as your max, randomly 99 + 1 will make the code generate 100, so if you really want max of 99, use 98 in this code. ๐Ÿ˜€ ยท * @author balcopc */ import java.util.Random; public class RandomNumberProj {
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ java โ€บ java-math-random-method-examples
Java Math random() Method - GeeksforGeeks
December 20, 2025 - The Math.random() method in Java is used to generate a pseudorandom double value that is greater than or equal to 0.0 and less than 1.0. Internally, this method uses a single instance of java.util.Random to produce random values, making it suitable ...
๐ŸŒ
Sentry
sentry.io โ€บ sentry answers โ€บ java โ€บ how do i generate random integers within a specific range in java?
How do I generate random integers within a specific range in Java? | Sentry
Finally, we can generate a random number using the java.lang.Math class. The Math class provides a random() method, but this method returns a double value from 0.0 (inclusive) to 1.0 (exclusive).
๐ŸŒ
Quora
quora.com โ€บ How-can-you-generate-random-integers-between-0-and-9-in-Java
How to generate random integers between 0 and 9 in Java - Quora
Answer (1 of 6): In Java there are a few ways to do this and the two popular ways would be: * java.util.Random [code]import java.util.Random; public class generateRandom{ public static void main(String args[]) { // create instance of Random class Random rand = new Random()...
๐ŸŒ
W3Schools
w3schools.com โ€บ java โ€บ ref_math_random.asp
Java Math random() Method
Java Examples Java Videos Java Compiler Java Exercises Java Quiz Java Code Challenges Java Server Java Syllabus Java Study Plan Java Interview Q&A Java Certificate ... The random() method returns a random number between 0 and 1.
๐ŸŒ
Tutorialspoint
tutorialspoint.com โ€บ java โ€บ lang โ€บ math_random.htm
Java - Math random() method
The following example shows the ... void main(String[] args) { // get two random double numbers double x = Math.random() * 5.0; double y = Math.random() * 5.0; // print the numbers and print the higher one System.out.print...
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ java โ€บ generating-random-numbers-in-java
Generating Random Numbers in Java - GeeksforGeeks
April 24, 2025 - ... // Java program to demonstrate ... static void main(String args[]) { // Generating random doubles System.out.println("Random doubles: " + Math.random()); System.out.println("Random doubles: " + Math.random()); } ...
๐ŸŒ
DZone
dzone.com โ€บ data engineering โ€บ data โ€บ random number generation in java
Guide to Random Number Generation in Java
December 26, 2017 - The random() method returns a double value with a positive sign, greater than or equal to 0.0 and less than 1.0. When you call Math.random(), under the hood, a java.util.Random pseudorandom-number generator object is created and used.