Table of contents
1.
Introduction
2.
Using the java.util.Random Class
2.1.
Syntax of Random Class
3.
Generating Random Numbers with Random Class
3.1.
Example
3.2.
Java
4.
Using Math.random() Method
4.1.
Syntax of Math.random()
5.
Generating Random Numbers with Math.random()
5.1.
Generating Random Integers within a Range
6.
Examples with Math.random()
6.1.
Example 1: Generating Random Double
6.2.
Java
6.3.
Example 2: Generating Random Integer within a Range
6.4.
Java
7.
Using ThreadLocalRandom Class
7.1.
Syntax of ThreadLocalRandom
8.
Generating Random Numbers with ThreadLocalRandom
8.1.
Example
9.
Java
10.
Frequently Asked Questions
10.1.
What is the difference between Random and SecureRandom?
10.2.
How do I generate random numbers in a specific range?
10.3.
Can Math.random() be used for secure random number generation?
11.
Conclusion
Last Updated: Sep 29, 2024
Easy

How to Generate a Random Number in Java?

Author Pallavi singh
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Generating random numbers is a common requirement in programming. Whether you're developing a game, creating simulations, or handling any application that requires randomness, knowing how to generate random numbers in Java is essential. 

How to Generate a Random Number in Java?

This article will guide you through different ways to generate random numbers in Java, covering various methods with syntax and examples.

Using the java.util.Random Class

The java.util.Random class provides methods to generate random numbers of different types, such as integers, doubles, and booleans. It's part of the java.util package and is widely used for generating random numbers in Java.

Syntax of Random Class

To use the Random class, you first need to create an instance of it:

import java.util.Random;
Random random = new Random();

Generating Random Numbers with Random Class

To generate a random integer:

int randomInt = random.nextInt();


To generate a random integer within a specific range (e.g., 0 to 99):

int randomIntInRange = random.nextInt(100);

Example

  • Java

Java

import java.util.Random;

public class RandomExample {

   public static void main(String[] args) {

       Random random = new Random();

       double randomDouble = random.nextDouble();

       System.out.println("Random Double: " + randomDouble);

   }

}
You can also try this code with Online Java Compiler
Run Code


Output

Random Double: 0.4644784159902743

Using Math.random() Method

The Math.random() method returns a double value between 0.0 (inclusive) and 1.0 (exclusive). It's a simple way to generate random numbers without creating an instance of any class.

Syntax of Math.random()

double randomValue = Math.random();

Generating Random Numbers with Math.random()

To generate a random double:

double randomDouble = Math.random();

Generating Random Integers within a Range

To generate a random integer within a specific range (e.g., 0 to 99):

int randomIntInRange = (int) (Math.random() * 100);

Examples with Math.random()

Example 1: Generating Random Double

  • Java

Java

public class MathRandomExample {

   public static void main(String[] args) {

       double randomDouble = Math.random();

       System.out.println("Random Double: " + randomDouble);

   }

}
You can also try this code with Online Java Compiler
Run Code

 

Output

Random Double: 0.9651842982270894

Example 2: Generating Random Integer within a Range

  • Java

Java

public class MathRandomExample {

   public static void main(String[] args) {

       int randomInt = (int) (Math.random() * 100);

       System.out.println("Random Integer: " + randomInt);

   }

}
You can also try this code with Online Java Compiler
Run Code


Output

Random Integer: 43

Using ThreadLocalRandom Class

The ThreadLocalRandom class is designed for use in multi-threaded environments. It's more efficient than Random when used within a single thread.

Syntax of ThreadLocalRandom

To use ThreadLocalRandom, you can call its static methods:

import java.util.concurrent.ThreadLocalRandom;
int randomInt = ThreadLocalRandom.current().nextInt();

Generating Random Numbers with ThreadLocalRandom

To generate a random integer:

int randomInt = ThreadLocalRandom.current().nextInt();

Example

  • Java

Java

import java.util.concurrent.ThreadLocalRandom;

public class ThreadLocalRandomExample {
public static void main(String[] args) {
int randomInt = ThreadLocalRandom.current().nextInt();
System.out.println("Random Integer: " + randomInt);
}
}
You can also try this code with Online Java Compiler
Run Code


Output

Random Integer: 870004142

Frequently Asked Questions

What is the difference between Random and SecureRandom?

Random is suitable for general-purpose use, while SecureRandom provides cryptographically strong random numbers for security-sensitive applications.

How do I generate random numbers in a specific range?

Use nextInt(bound) with Random or ThreadLocalRandom, and (int) (Math.random() * range) with Math.random().

Can Math.random() be used for secure random number generation?

No, Math.random() is not suitable for security-sensitive applications. Use SecureRandom instead.

Conclusion

Generating random numbers in Java is a versatile task that can be accomplished using various classes and methods. Whether you need simple random numbers for a game or secure random numbers for encryption, Java provides the tools you need. By understanding and applying these methods, you can effectively generate random numbers for your applications.

You can also practice coding questions commonly asked in interviews on Coding Ninjas Code360

Also, check out some of the Guided Paths on topics such as Data Structure and AlgorithmsCompetitive ProgrammingOperating SystemsComputer Networks, DBMSSystem Design, etc., as well as some Contests, Test Series, and Interview Experiences curated by top Industry Experts.

Live masterclass