---
title: "How to create random numbers between given ranges in Java?"  
description: "How to create random numbers between given ranges in Java?"  
author: "Utpal Vishwas"  
published: 2023-07-21  
updated: 2023-07-22  
canonical: https://www.mindstick.com/forum/159213/how-to-create-random-numbers-between-given-ranges-in-java  
category: "java"  
tags: ["java"]  
reading_time: 3 minutes  

---

# How to create random numbers between given ranges in Java?

How to create [random numbers](https://www.mindstick.com/forum/832/generate-random-numbers-uniformly-over-entire-range) between given ranges in [Java](https://www.mindstick.com/articles/12214/web-development-company-in-india-laid-on-the-foundation-of-concrete-java-programming)?

## Replies

### Reply by Aryan Kumar

There are a few ways to create [random](https://www.mindstick.com/forum/33419/generating-random-numbers-in-objective-c) numbers between given ranges in Java.

Here's a method using the `Math.random()` function:

```plaintext
public class RandomNumbers {

  public static void main(String[] args) {
    int min = 1;
    int max = 10;

    // Generate a random number between 1 and 10
    int randomNumber = (int) (Math.random() * (max - min + 1) + min);

    System.out.println(randomNumber);
  }
}
```

The `Math.random()` function returns a random number between 0 and 1. To generate a random number between a specified range, you can multiply the result of the `Math.random()` function by the difference between the maximum and minimum values, and then add the minimum value.

Here's a method using the `Random` class:

```plaintext
import java.util.Random;

public class RandomNumbers {

  public static void main(String[] args) {
    int min = 1;
    int max = 10;

    // Create a Random object
    Random random = new Random();

    // Generate a random number between 1 and 10
    int randomNumber = random.nextInt(max - min + 1) + min;

    System.out.println(randomNumber);
  }
}
```

The `Random` class provides a number of methods for generating random numbers. The `nextInt()` method generates a random integer between 0 and the specified maximum value. To generate a random number between a specified range, you can pass the difference between the maximum and minimum values to the `nextInt()` method.

Here's a method using the `ThreadLocalRandom` class:

```plaintext
import java.util.concurrent.ThreadLocalRandom;

public class RandomNumbers {

  public static void main(String[] args) {
    int min = 1;
    int max = 10;

    // Create a ThreadLocalRandom object
    ThreadLocalRandom random = ThreadLocalRandom.current();

    // Generate a random number between 1 and 10
    int randomNumber = random.nextInt(max - min + 1) + min;

    System.out.println(randomNumber);
  }
}
```

The `ThreadLocalRandom` class provides a thread-safe way to generate random numbers. The `nextInt()` method generates a random integer between 0 and the specified maximum value. To generate a random number between a specified range, you can pass the difference between the maximum and minimum values to the `nextInt()` method.

Which method you use to create random numbers between given ranges depends on your specific needs. If you are generating random numbers for a single thread, then you can use the `Random` class. If you are generating random numbers for multiple threads, then you can use the `ThreadLocalRandom` class.

Here are some important things to note about generating random numbers between given ranges:

- The random numbers that are generated are pseudorandom, which means that they are not truly random.
- The random numbers that are generated are repeatable, which means that you can generate the same sequence of random numbers by calling the `nextInt()` method with the same arguments.


---

Original Source: https://www.mindstick.com/forum/159213/how-to-create-random-numbers-between-given-ranges-in-java

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
