0
0
JavaHow-ToBeginner · 3 min read

How to Generate Random Number in Java: Simple Guide

In Java, you can generate random numbers using the java.util.Random class or the Math.random() method. The Random class provides more control and methods, while Math.random() returns a double between 0.0 and 1.0.
📐

Syntax

To generate random numbers in Java, you mainly use two approaches:

  • Using Random class: Create an instance of Random and call methods like nextInt() or nextDouble().
  • Using Math.random() method: Call Math.random() which returns a double between 0.0 (inclusive) and 1.0 (exclusive).

Example syntax:

Random rand = new Random();
int num = rand.nextInt(100); // random int from 0 to 99

// or

double num = Math.random(); // random double from 0.0 to 1.0
java
import java.util.Random;

public class RandomSyntax {
    public static void main(String[] args) {
        Random rand = new Random();
        int randomInt = rand.nextInt(50); // 0 to 49
        double randomDouble = rand.nextDouble(); // 0.0 to 1.0

        double mathRandom = Math.random(); // 0.0 to 1.0

        System.out.println("Random int (0-49): " + randomInt);
        System.out.println("Random double (0.0-1.0) from Random: " + randomDouble);
        System.out.println("Random double (0.0-1.0) from Math.random(): " + mathRandom);
    }
}
Output
Random int (0-49): 23 Random double (0.0-1.0) from Random: 0.734567891234 Random double (0.0-1.0) from Math.random(): 0.456789123456
💻

Example

This example shows how to generate a random integer between 1 and 100 using both Random and Math.random(). It prints the results to the console.

java
import java.util.Random;

public class RandomExample {
    public static void main(String[] args) {
        Random random = new Random();

        // Random integer from 1 to 100
        int randomInt = random.nextInt(100) + 1;

        // Using Math.random() to get int from 1 to 100
        int mathRandomInt = (int)(Math.random() * 100) + 1;

        System.out.println("Random integer using Random class: " + randomInt);
        System.out.println("Random integer using Math.random(): " + mathRandomInt);
    }
}
Output
Random integer using Random class: 57 Random integer using Math.random(): 23
⚠️

Common Pitfalls

Common mistakes when generating random numbers in Java include:

  • Not adding 1 when you want a range starting from 1 (e.g., nextInt(100) returns 0 to 99, so add 1 to get 1 to 100).
  • Using Math.random() without casting properly to int, which can cause unexpected results.
  • Creating a new Random object inside a loop, which can reduce randomness.
java
import java.util.Random;

public class RandomPitfalls {
    public static void main(String[] args) {
        // Wrong: new Random inside loop
        for (int i = 0; i < 3; i++) {
            Random randWrong = new Random();
            System.out.println("Wrong random int: " + randWrong.nextInt(10));
        }

        // Right: create Random once
        Random randRight = new Random();
        for (int i = 0; i < 3; i++) {
            System.out.println("Right random int: " + randRight.nextInt(10));
        }
    }
}
Output
Wrong random int: 3 Wrong random int: 3 Wrong random int: 3 Right random int: 7 Right random int: 1 Right random int: 9
📊

Quick Reference

Summary tips for generating random numbers in Java:

MethodDescriptionRange Example
Random.nextInt(n)Returns int from 0 (inclusive) to n (exclusive)nextInt(10) → 0 to 9
Random.nextDouble()Returns double from 0.0 (inclusive) to 1.0 (exclusive)0.0 to 1.0
Math.random()Returns double from 0.0 (inclusive) to 1.0 (exclusive)0.0 to 1.0
(int)(Math.random() * n)Cast to int for int range 0 to n-1(int)(Math.random() * 10) → 0 to 9

Key Takeaways

Use java.util.Random for flexible random number generation with many methods.
Math.random() returns a double between 0.0 and 1.0 and needs casting for integers.
Add 1 to nextInt(n) if you want a range starting from 1 instead of 0.
Create a single Random instance and reuse it to avoid poor randomness.
Casting Math.random() results incorrectly can cause unexpected values.