How to Generate Random Numbers in Java


By squashlabs, Last Updated: November 2, 2023

How to Generate Random Numbers in Java

To generate random numbers in Java, you can use the java.util.Random class. This class provides methods for generating random numbers of different data types. Here are two possible approaches:

Approach 1: Using the Random class

1. Create an instance of the Random class:

Random random = new Random();

2. Use the appropriate method from the Random class to generate random numbers based on your requirements. Here are some common examples:
– To generate a random integer between 0 and a specified upper bound (exclusive), use the nextInt(int bound) method:

int randomNumber = random.nextInt(100); // generates a random number between 0 and 99

– To generate a random long, use the nextLong() method:

long randomNumber = random.nextLong();

– To generate a random double between 0.0 (inclusive) and 1.0 (exclusive), use the nextDouble() method:

double randomNumber = random.nextDouble();

– To generate a random boolean, use the nextBoolean() method:

boolean randomBoolean = random.nextBoolean();

Related Article: How to Convert JsonString to JsonObject in Java

Approach 2: Using the Math.random() method

Java also provides the Math.random() method, which returns a random double between 0.0 (inclusive) and 1.0 (exclusive). To generate random numbers of other types, you can use this method in combination with type casting and arithmetic operations. Here are some examples:

– To generate a random integer between a specified lower bound (inclusive) and upper bound (exclusive), use the following formula:

int randomNumber = (int) (Math.random() * (upperBound - lowerBound) + lowerBound);

For example, to generate a random number between 1 and 10 (both inclusive), you can use:

int randomNumber = (int) (Math.random() * 10 + 1);

– To generate a random double between a specified lower bound (inclusive) and upper bound (exclusive), use the following formula:

double randomNumber = Math.random() * (upperBound - lowerBound) + lowerBound;

For example, to generate a random number between 0.0 (inclusive) and 1.0 (exclusive), you can use:

double randomNumber = Math.random();

Best Practices and Considerations

– If you need to generate multiple random numbers, it is recommended to reuse the same instance of the Random class rather than creating a new instance each time. This helps ensure better randomness.
– If you are generating random numbers in a multithreaded environment, consider using ThreadLocalRandom instead of Random to avoid potential contention and synchronization issues.
– When generating random numbers within a specific range, be mindful of the boundary conditions and inclusive/exclusive nature of the upper and lower bounds.
– If you require cryptographically secure random numbers, consider using the class instead of Random. SecureRandom provides a more secure source of random numbers, but it is generally slower than Random.

Example: Generating Random Numbers within a Range

Here’s an example that generates 10 random integers between 1 and 100 (both inclusive) using the Random class:

import java.util.Random;

public class RandomNumberGenerator {
    public static void main(String[] args) {
        Random random = new Random();
        for (int i = 0; i < 10; i++) {
            int randomNumber = random.nextInt(100) + 1;

This code creates an instance of Random and uses the nextInt(int bound) method to generate random integers between 0 and 99. The + 1 ensures that the generated numbers are between 1 and 100. The loop iterates 10 times to generate and print 10 random numbers.

Related Article: How to Implement a Delay in Java Using java wait seconds

Alternative Ideas

– If you need to generate random numbers with a specific distribution (e.g., Gaussian distribution), you can explore libraries like Apache Commons Math or Jama, which provide advanced mathematical functions for random number generation.
– If you require random numbers for cryptographic purposes, consider using the class or a dedicated cryptographic library such as Bouncy Castle.

For more information, you can refer to the official Java documentation:
– [java.util.Random](
– [java.lang.Math](

How to Convert a String to Date in Java

This article provides a step-by-step guide on how to convert a string to a date in Java. It covers two approaches: using SimpleDateFormat and using DateTimeFormatter... read more

How to Find the Max Value of an Integer in Java

This article provides a simple guide to finding the maximum integer value in Java. It covers various methods, including using the Integer.MAX_VALUE constant and the... read more

How to Use the JsonProperty in Java

This article dives into the purpose and usage of @JsonProperty in Java coding. It covers topics such as mapping Java fields to JSON properties, ignoring fields during... read more

How to Set the JAVA_HOME in Linux for All Users

Setting JAVA_HOME in Linux for all users can be achieved through two different methods. The first method involves setting JAVA_HOME in the /etc/environment file, while... read more

How to Compare Strings in Java

This article provides a simple guide on comparing strings in Java programming language. It covers topics such as using the equals() method and the compareTo() method.... read more

How to Change the Date Format in a Java String

Adjusting date format in a Java string can be done using two approaches: Using SimpleDateFormat or using DateTimeFormatter (Java 8 and later). Both methods provide ways... read more