0
0
JavaProgramBeginner · 2 min read

Java Program to Generate Random Number

You can generate a random number in Java using Random rand = new Random(); int num = rand.nextInt(); which gives a random integer, or use rand.nextInt(bound) for a number within a range.
📋

Examples

InputNo input needed
OutputRandom number: 123456789
InputNo input needed
OutputRandom number between 0 and 9: 7
InputNo input needed
OutputRandom number between 0 and 99: 42
🧠

How to Think About It

To generate a random number, you create an object that can produce random values. Then you ask it for a number either in the full integer range or within a specific limit. This mimics picking a random card from a deck or rolling a dice with many sides.
📐

Algorithm

1
Create a Random object
2
Call the method to get a random number
3
If needed, specify the upper limit for the random number
4
Print the random number
💻

Code

java
import java.util.Random;

public class RandomNumberExample {
    public static void main(String[] args) {
        Random rand = new Random();
        int randomNum = rand.nextInt(100); // random number between 0 and 99
        System.out.println("Random number between 0 and 99: " + randomNum);
    }
}
Output
Random number between 0 and 99: 42
🔍

Dry Run

Let's trace generating a random number between 0 and 99 through the code

1

Create Random object

Random rand = new Random(); // rand is ready to generate numbers

2

Generate random number

int randomNum = rand.nextInt(100); // randomNum gets a value like 42

3

Print the number

System.out.println("Random number between 0 and 99: " + randomNum); // prints the number

StepVariableValue
1randRandom object created
2randomNum42
3OutputRandom number between 0 and 99: 42
💡

Why This Works

Step 1: Create Random object

The Random class provides methods to generate random numbers. Creating an object prepares it to produce random values.

Step 2: Generate random number within range

Calling nextInt(100) returns a random integer from 0 up to 99, simulating picking a number from a set range.

Step 3: Print the result

The program prints the random number so you can see the output on the screen.

🔄

Alternative Approaches

Math.random()
java
public class RandomNumberMath {
    public static void main(String[] args) {
        int randomNum = (int)(Math.random() * 100);
        System.out.println("Random number between 0 and 99: " + randomNum);
    }
}
Uses <code>Math.random()</code> which returns a double between 0.0 and 1.0; multiply and cast to int for range. Simpler but less flexible than Random class.
ThreadLocalRandom
java
import java.util.concurrent.ThreadLocalRandom;

public class RandomNumberThreadLocal {
    public static void main(String[] args) {
        int randomNum = ThreadLocalRandom.current().nextInt(100);
        System.out.println("Random number between 0 and 99: " + randomNum);
    }
}
ThreadLocalRandom is efficient for multithreaded programs and avoids creating new Random objects.

Complexity: O(1) time, O(1) space

Time Complexity

Generating a random number is a constant time operation because it does not depend on input size or loops.

Space Complexity

Only a small fixed amount of memory is used for the Random object and the integer variable.

Which Approach is Fastest?

Using ThreadLocalRandom can be faster in multithreaded environments, while Math.random() is simpler but less flexible.

ApproachTimeSpaceBest For
Random classO(1)O(1)General purpose random numbers
Math.random()O(1)O(1)Simple random numbers without extra imports
ThreadLocalRandomO(1)O(1)Multithreaded applications
💡
Use nextInt(bound) to get a random number within a specific range starting from zero.
⚠️
Beginners often forget that nextInt(bound) excludes the upper bound, so the max number is one less than the bound.