The simplest way to create a random number in Java
Java has a built-in class called Random that generates random numbers for you. To use it, you create a new Random object, then call a method on it to get the type of number you want — an integer, a decimal, or something else.
Here is the most common approach: import the Random class at the top of your file, create a new Random object, and call nextInt() to get a random whole number.
If you want a random number between 1 and 100, you write:
import java.util.Random; Random rand = new Random(); int randomNumber = rand.nextInt(100) + 1;
The nextInt(100) part gives you a number from 0 to 99. Adding 1 shifts the range to 1 to 100. If you want 0 to 99 instead, just use nextInt(100) without the + 1.
Key Takeaways
- Import java.util.Random at the top of your file, then create a new Random object to start generating numbers.
- Use nextInt(n) to get a random whole number from 0 to n-1, so nextInt(100) gives 0 to 99.
- Use nextDouble() to get a random decimal number between 0.0 and 1.0.
- If you need the same random sequence every time you run the code (for testing), pass a seed number to the Random constructor: new Random(42).
Getting different types of random numbers
The Random class has several methods for different number types. nextInt() gives whole numbers, nextDouble() gives decimals, and nextLong() gives very large whole numbers.
If you want a random decimal between 0.0 and 1.0, use nextDouble():
double randomDecimal = rand.nextDouble();
To get a random decimal in a specific range — say, 1.5 to 10.5 — multiply and add:
double randomDecimal = rand.nextDouble() * 9.0 + 1.5;
This works because nextDouble() always returns a number from 0.0 to just under 1.0. Multiplying by 9.0 stretches it to 0.0 to just under 9.0, then adding 1.5 shifts it to 1.5 to just under 10.5.
Using Math.random() as a shortcut
Java also has a static method called Math.random() that does not require you to create a Random object. It returns a decimal between 0.0 and 1.0, similar to nextDouble().
For a quick random number, Math.random() is shorter to write:
int randomNumber = (int)(Math.random() * 100) + 1;
This converts the decimal result to a whole number by casting it to int, multiplies by 100 to get a range of 0 to 99, then adds 1 to shift to 1 to 100. The trade-off is that Math.random() creates a new Random object behind the scenes each time you call it, which is slightly less efficient if you need many random numbers. For a single number or a few numbers, the difference does not matter.
Controlling randomness with a seed
Normally, each time you run your program, the Random object produces a different sequence of numbers. For testing or debugging, you may want the same sequence every time. You do this by passing a seed — a starting number — to the Random constructor.
If you create new Random(42), the sequence will be identical every time you run the program with that seed. Change the seed to 99, and you get a different but still repeatable sequence.
Random rand = new Random(42); System.out.println(rand.nextInt(100)); System.out.println(rand.nextInt(100)); System.out.println(rand.nextInt(100));
Run this code twice and you will see the same three numbers both times. This is useful when you are writing tests and need to verify that your code behaves the same way given the same random input.
Generating random numbers in a specific range
The formula for a random number in a specific range depends on whether you want whole numbers or decimals. For whole numbers from a minimum to a maximum, use this pattern:
int min = 10; int max = 50; int randomNumber = rand.nextInt(max - min + 1) + min;
This gives you a number from 10 to 50 inclusive. The key is nextInt(max - min + 1), which gives 0 to (max - min), then adding min shifts the range to start at min.
For decimals in a range, multiply and add the same way as before:
double min = 5.5; double max = 15.5; double randomNumber = rand.nextDouble() * (max - min) + min;
When to use Random versus Math.random()
Use the Random class when you need many random numbers or when you want to control the seed for testing. Creating one Random object and reusing it is more efficient than calling Math.random() repeatedly.
Use Math.random() when you need just one or two random numbers and do not want to import anything or create an object. It is simpler to read for a single line of code.
In Java 17 and later, there is also a RandomGenerator interface that offers more advanced options, but Random is still the standard choice for most programs.
Common mistakes and how to avoid them
The most common mistake is forgetting that nextInt(n) returns 0 to n-1, not 1 to n. If you want 1 to 100, you must add 1. If you want 0 to 99, do not add anything.
Another mistake is creating a new Random object inside a loop. This wastes memory and can produce poor randomness because the objects are created too quickly. Create the Random object once outside the loop, then call its methods inside the loop.
A third mistake is casting Math.random() to an integer incorrectly. Always multiply first, then cast: (int)(Math.random() * 100), not (int)Math.random() * 100. The second version casts 0.0 to 1.0 to the integer 0, then multiplies by 100, giving you only 0.
Frequently Asked Questions
Can I generate a random number between two specific numbers?
Yes. Use the formula rand.nextInt(max - min + 1) + min for whole numbers, or rand.nextDouble() * (max - min) + min for decimals. For example, rand.nextInt(51 - 10 + 1) + 10 gives you 10 to 50.
What is the difference between Random and Math.random()?
Random is a class you create once and reuse, which is more efficient for multiple numbers. Math.random() is a static method that creates a new Random object each time, making it simpler for a single number but slower if called many times in a loop.
Why do I get the same random numbers every time I run my program?
You probably passed a seed to the Random constructor, like new Random(42). Seeds make the sequence repeatable for testing. Remove the seed or use new Random() with no argument to get different numbers each time.
How do I generate a random boolean (true or false)?
Use rand.nextBoolean(), which returns true or false with equal probability. No parameters needed.
Is Math.random() truly random?
Math.random() and the Random class use a pseudorandom algorithm, meaning they produce numbers that appear random but are actually generated by a mathematical formula. For most programs, this is random enough. For cryptography or security, use SecureRandom instead.