As the documentation says, this method call returns "a pseudorandom, uniformly distributed int value between 0 (inclusive) and the specified value (exclusive)". This means that you will get numbers from 0 to 9 in your case. So you've done everything correctly by adding one to that number.

Generally speaking, if you need to generate numbers from min to max (including both), you write

random.nextInt(max - min + 1) + min
Answer from Malcolm on Stack Overflow
๐ŸŒ
Coderanch
coderanch.com โ€บ t โ€บ 650127 โ€บ java โ€บ create-simple-random-number-generator
How do I create a simple random number generator for the number between 1 -10? (Beginning Java forum at Coderanch)
There are a couple of ways in java. First in the class java.lang.Math it has a method random() that gives you a number between 0 and 1. You can multiply that to get the range you are after.
Discussions

How do I generate random integers within a specific range in Java? - Stack Overflow
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: ... And there you have it. A random integer value in the range [Min,Max], or per the example [5,10]: ... Lilian A. Moraru ยท Lilian A. Moraru Over a year ago ยท The Sun documentation explicitly says that you should better use Random() if you need an int instead of Math... More on stackoverflow.com
๐ŸŒ stackoverflow.com
How can I get a random number between 1 and 12?
Please ensure that: Your code is properly formatted as code block - see the sidebar (About on mobile) for instructions You include any and all error messages in full You ask clear questions You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions. Trying to solve problems on your own is a very important skill. Also, see Learn to help yourself in the sidebar If any of the above points is not met, your post can and will be removed without further warning. Code is to be formatted as code block (old reddit: empty line before the code, each code line indented by 4 spaces, new reddit: https://imgur.com/a/fgoFFis ) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc. Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit. Code blocks look like this: public class HelloWorld { public static void main(String[] args) { System.out.println("Hello World!"); } } You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above. If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures. To potential helpers Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice. I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns. More on reddit.com
๐ŸŒ r/javahelp
19
5
December 9, 2020
Java Generate Random Number Between Two Given Values - Stack Overflow
I would like to know how to generate a random number between two given values. I am able to generate a random number with the following: Random r = new Random(); for(int i = 0; i More on stackoverflow.com
๐ŸŒ stackoverflow.com
How the code: int n = (int) (Math.random() * 62) uniformly generates a random number between 0 & 61
One fiftieth of your 0-1 range is above 0.98. So the chance of getting 61 is less than one fiftieth... which is exactly what you would expect. More on reddit.com
๐ŸŒ r/learnjava
4
1
November 19, 2023
๐ŸŒ
W3Schools
w3schools.com โ€บ java โ€บ java_howto_random_number.asp
Java How To Generate Random Numbers
Java Examples Java Videos Java ... Math.random() method to generate a random number. Math.random() returns a random number between 0.0 (inclusive), and 1.0 (exclusive): Math.random(); Try it Yourself ยป ยท...
๐ŸŒ
Java67
java67.com โ€บ 2015 โ€บ 01 โ€บ how-to-get-random-number-between-0-and-1-java.html
How to Generate Random Number between 1 to 10 - Java Example | Java67
Each has their own pros and cons ... by using Math.random() method. This method returns a pseudorandom positive double value between 0.0 and 1.0, where 0.0 is inclusive and 1.0 is exclusive....
๐ŸŒ
DigitalOcean
digitalocean.com โ€บ community โ€บ tutorials โ€บ random-number-generator-java
Random Number Generator in Java | DigitalOcean
August 4, 2022 - Also, 0 is included in the generated random number, so we have to keep calling nextInt method until we get a value between 1 and 10. You can extend the above code to generate the random number within any given range. We can use Math.random() or Random class nextDouble method to generate random ...
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ java โ€บ java-math-random-method-examples
Java Math random() Method - GeeksforGeeks
December 20, 2025 - Now to get random integer numbers from a given fixed range, we take a min and max variable to define the range for our random numbers, both min and max are inclusive in the range. java ยท class Gfg{ public static void main(String[] args) { int ...
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))
Find elsewhere
๐ŸŒ
Delft Stack
delftstack.com โ€บ home โ€บ howto โ€บ java โ€บ java random number between 1 and 10
How to Generate a Random Number Between 1 and 10 in Java | Delft Stack
February 2, 2024 - This method is part of the Math ... is required. Unlike the Random class, Math.random() directly returns a random double value between 0 (inclusive) and 1 (exclusive)....
๐ŸŒ
Quora
quora.com โ€บ How-do-I-generate-a-random-number-in-Java-between-1-and-10
How to generate a random number in Java between 1 and 10 - Quora
Answer (1 of 7): Itโ€™s possible to use Array Lists or switch case statements to generate numbers 1โ€“10, but another way is to simply use two arrays. The code I wrote below instantiates an array, randomizes the order of the numbers 1โ€“10, stores the data into another array, and then prints ...
๐ŸŒ
Coderanch
coderanch.com โ€บ t โ€บ 622918 โ€บ java โ€บ convert-Math-random-generate-random
How can I convert Math.random to generate a random number between 1-1000 (Beginning Java forum at Coderanch)
For example, to generate an integer between 0 and 9, you would write: int number = (int)(Math.random() * 10); By multiplying the value by 10, the range of possible values becomes 0.0 <= number < 10.0. Using Math.random works well when you need to generate a single random number.
๐ŸŒ
Arrowhitech
blog.arrowhitech.com โ€บ java-random-number-between-1-and-10
Java random number between 1 and 10: How to generate it โ€“ Blogs | AHT Tech | Digital Commerce Experience Company
Additionally, the random number between 1 and 10 java may be repeated. Because we do not have a big range of numbers. ... You can also use the Math.random() method to generate a android random number between 1 and 10 using the Math.random() method. So you can use Math.
๐ŸŒ
Sabe
sabe.io โ€บ blog โ€บ java-random-number-between-1-and-10
How to generate a Random Number between 1 and 10 in Java
May 13, 2022 - Another way to generate random numbers is to use the Math class. This class contains a random() method that returns a random float between 0 and 1. This is useful because we just need to do a simple multiplication to get a random number between 1 and 10. ... JAVApublic class Main { public static void main(String[] args) { Random random = new Random(); int min = 1; int max = 10; int value = (int) (Math.random() * (max - min)) + min...
๐ŸŒ
Vultr Docs
docs.vultr.com โ€บ java โ€บ standard-library โ€บ java โ€บ lang โ€บ Math โ€บ random
Java Math random() - Generate Random Number | Vultr Docs
September 27, 2024 - The value lies between 0.0 and 1.0. Use Math.random() in conjunction with type casting to generate a random integer. ... int n = 10; // Example range limit int randomInt = (int)(Math.random() * n); System.out.println("Random Integer from 0 to ...
๐ŸŒ
DEV Community
dev.to โ€บ meenakshi052003 โ€บ generating-random-numbers-between-1-and-100-in-java-i3b
Generating Random Numbers Between 1 and 100 in Java - DEV Community
March 5, 2024 - The Random class uses a seed value to initialize its pseudorandom number generator. If the same seed is used, the sequence of generated numbers will be identical. If no seed is provided, the current system time is used as the default seed. import java.util.Random; public class SeedExample { public static void main(String[] args) { long seed = 123456L; Random randomWithSeed = new Random(seed); int randomNumber = randomWithSeed.nextInt(100) + 1; System.out.println("Random Number with Seed: " + randomNumber); } }
๐ŸŒ
Codecademy
codecademy.com โ€บ forum_questions โ€บ 4f2ab50106bc4500010080ef
Why I can't use Math.random()*11 instead of Math.random()*10+1 | Codecademy
And when I use Math.random in conjunction with Math.floor the value 10.9999 is round down to 10. ... Math.random() gives numbers between 0 and 1, but NOT 1. So Math.random()*11 will give numbers between 0 and 11, but not 11.
๐ŸŒ
Medium
medium.com โ€บ @generativeai.saif โ€บ java-random-number-generator-5-methods-explained-with-examples-bf9e53abed3c
Java Random Number Generator: 5 Methods Explained with Examples | by Saif Ali | Medium
April 6, 2025 - public class MathRandomExample { public static void main(String[] args) { // Generate a random double between 0.0 and 1.0 double randomValue = Math.random(); System.out.println("Random Value: " + randomValue); // Generate a random integer between 0 and 100 int randomInt = (int)(Math.random() * 101); System.out.println("Random Integer (0-100): " + randomInt); // Generate a random number within a specific range (min to max) int min = 50; int max = 100; int randomInRange = min + (int)(Math.random() * ((max - min) + 1)); System.out.println("Random number between " + min + " and " + max + ": " + randomInRange); } } Behind the scenes, Math.random() uses an instance of java.util.Random to generate random numbers, but it provides a more convenient interface for simple use cases.
๐ŸŒ
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 - Here is a Java code example of ... 10 is exclusive. This code uses Math.random() method, which returns pseudo-random number in a range 0.0 to 1.0, where later is exclusive, by multiplying output with and then type casting into int, we can generate random integers in any ...
๐ŸŒ
Mkyong
mkyong.com โ€บ home โ€บ java โ€บ java โ€“ generate random integers in a range
Java - Generate random integers in a range - Mkyong.com
August 19, 2015 - package com.mkyong.example.test; ... r.nextInt((max - min) + 1) + min; } } ... This Math.random() gives a random double from 0.0 (inclusive) to 1.0 (exclusive)....