Open In App

Generating Random Numbers in Java

Last Updated : 24 Apr, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Random numbers are widely used in programming for simulations, gaming, security, etc. There are multiple ways to generate random numbers using built-in methods and classes in Java. The most commonly used approaches are listed below:

  • java.util.Random class
  • Math.random() method (returns double values)
  • ThreadLocalRandom class (introduced in Java 7)

Let’s explore each of these approaches one by one in detail

1. Using java.util.Random

With the help of this class, we can generate random numbers of different types (int, double, long, boolean, etc.).

Key Methods:

  • nextInt(): This method generates a random integer (full range, including negatives)
  • nextInt(int bound): This method generates a random integer between 0 (inclusive) and bound (exclusive)
  • nextDouble(): This method generates a random double between 0.0 (inclusive) and 1.0 (exclusive)
  • nextBoolean(): Random true or false

Example:

Java
// Generating random number using java.util.Random;
import java.util.Random;

public class Geeks{
    
    public static void main(String[] args) {
        
        // Creating the instance of Random class
        Random r= new Random();

        // Generate random integers in range 0 to 999
        int r1 = r.nextInt(1000);
        int r2 = r.nextInt(1000);

        // Printing random integers
        System.out.println("Random Integers: " + r1);
        System.out.println("Random Integers: " + r2);

        // Generate random doubles
        double rd1 = r.nextDouble();
        double rd2 = r.nextDouble();

        // Printing random doubles
        System.out.println("Random Doubles: " + rd1);
        System.out.println("Random Doubles: " + rd2);
    }
}

Output
Random Integers: 39
Random Integers: 190
Random Doubles: 0.4200728082969115
Random Doubles: 0.9327571841228275

Generating Numbers in a Specific Range

The formula to generate a random number with a specific range is listed below:

Random rand = new Random();

int randomNum = rand.nextInt(max – min + 1) + min;

Note: min and max are our lower and higher limit of number.

Example:

Java
// Generating random number in a specific range 
import java.io.*;
import java.util.*;

class Geeks {
    
    public static void main (String[] args) {
        Random r = new Random();
        int max=100,min=50;
        System.out.println("Generated numbers are within "+ min +" to "+ max);
        System.out.println(r.nextInt(max - min + 1) + min);
        System.out.println(r.nextInt(max - min + 1) + min);
        System.out.println(r.nextInt(max - min + 1) + min);
    }
}

Output
Generated numbers are within 50 to 100
55
51
51


2. Using Math.random()

The Math class contains various methods for performing various numeric operations such as, calculating exponentiation, logarithms etc. One of these methods is random(), this method returns a double value with a positive sign, greater than or equal to 0.0 and less than 1.0. The returned values are chosen pseudo randomly. This method can only generate random numbers of type Doubles.

Example:

Java
// Java program to demonstrate working of 
// Math.random() to generate random numbers
import java.util.*;
  
public class Geeks
{
    public static void main(String args[])
    {
        // Generating random doubles
        System.out.println("Random doubles: " + Math.random());
        System.out.println("Random doubles: " + Math.random());
    }
}

Output
Random doubles: 0.38601320746656065
Random doubles: 0.9209882942481417

Generating Numbers in a Specific Range

Here is the formula to generate a random number with specific range, where min and max are our lower and higher limit of number:

int randomNum = min + (int)(Math.random() * ((max – min) + 1));

Example:

Java
// Demonstrating how to generate random 
// number within a specific range
import java.io.*;
import java.util.*;

class Geeks {
    
    public static void main (String[] args) {
        int max=100,min=50;
          System.out.println("Generated numbers are within "+min+" to "+max);
           System.out.println(min + (int)(Math.random() * ((max - min) + 1)));
          System.out.println(min + (int)(Math.random() * ((max - min) + 1)));
          System.out.println(min + (int)(Math.random() * ((max - min) + 1)));    
    }
}

Output
Generated numbers are within 50 to 100
82
68
77


3. Using ThreadLocalRandom

This is the recommended way in multi-threaded environments as it reduces contention.

Key Methods:

  • current().nextInt(): Random integer (full range)
  • current().nextInt(min, max + 1): Random integer in range (I have added this bounded example)
  • current().nextDouble(): Random double (0.0 to 1.0)
  • current().nextBoolean(): Random true or false

Example:

Java
// Demonstrates how to generate random integers, 
// doubles, and booleans using ThreadLocalRandom
import java.util.concurrent.ThreadLocalRandom;

public class Geeks {
    public static void main(String[] args) {
        // Generate random integers in range 0 to 999

        // Random number between 0 and 999
        int r1 = ThreadLocalRandom.current().nextInt(1000);
        
        // Random number between 0 and 999
        int r2 = ThreadLocalRandom.current().nextInt(1000); 

        // Print random integers
        System.out.println("Random Integers: " + r1);
        System.out.println("Random Integers: " + r2);

        // Generate Random doubles between 0.0 (inclusive) 
        // and 1.0 (exclusive)
        double rd1 = ThreadLocalRandom.current().nextDouble();
        double rd2 = ThreadLocalRandom.current().nextDouble();

        // Print random doubles
        System.out.println("Random Doubles: " + rd1);
        System.out.println("Random Doubles: " + rd2);

        // Generate random booleans
        boolean rb1 = ThreadLocalRandom.current().nextBoolean();
        boolean rb2 = ThreadLocalRandom.current().nextBoolean();

        // Print random Booleans
        System.out.println("Random Booleans: " + rb1);
        System.out.println("Random Booleans: " + rb2);
    }
}

Output
Random Integers: 812
Random Integers: 461
Random Doubles: 0.7420865203902993
Random Doubles: 0.9580683365617423
Random Booleans: false
Random Booleans: false


Article Tags :
Practice Tags :

Similar Reads