Generate a Random Number Between 1 and 10 in Java

Rupam Yadav Jan 30, 2023 Nov 01, 2020
  1. random.nextInt() to Generate a Random Number Between 1 and 10
  2. Math.random() to Generate Random Numbers Between 1 to 10
  3. ThreadLocalRandom.current.nextInt() to Generate Random Numbers Between 1 to 10
Generate a Random Number Between 1 and 10 in Java

We will look at the steps to generate a random number between 1 and 10 randomly in Java. We will see three Java packages or classes that can generate a random number between 1 and 10 and which of them is the most suitable one to use.

random.nextInt() to Generate a Random Number Between 1 and 10

java.util.Random is a package that comes with Java, and we can use it to generate a random number between a range. In our case, the range is 1 to 10.

This package has a class Random that allows us to generate multiple types of numbers, whether it is an int or a float. Check out the example to better understand.

import java.util.Random;

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

		Random random = new Random();

		int value = random.nextInt(max + min) + min;
		System.out.println(value);
    }
}

Output:

	6

To show that the above technique works and generates random numbers every time, we can use a loop to generate a new random number until it finishes. As we do not have a large range of numbers, the random numbers may be repeated.

import java.util.Random;

public class Main {
    public static void main(String[] args) {

        Random random = new Random();

        for(int i = 1; i <=10; i++) {
            int value = random.nextInt((10 - 1) + 1) + 1;
            System.out.println(value);
        }
    }

Output:

10
7
2
9
2
7
6
4
9

Math.random() to Generate Random Numbers Between 1 to 10

Another class that can help us achieve our goal is Math with multiple static functions to randomize numbers. We are going to use the random() method. It returns a random value of the float type. This is why we have to cast it into an int.

public class Main {
    public static void main(String[] args) {
		int min = 1;
		int max = 10;
		for(int i = min; i <=max; i++) {
			int getRandomValue = (int) (Math.random()*(max-min)) + min;
			System.out.println(getRandomValue);
		  
		}
    }

Output:

5
5
2
1
6
9
3
6
5
7

ThreadLocalRandom.current.nextInt() to Generate Random Numbers Between 1 to 10

The last method in our list to get random numbers between 1 and 10 is using the class ThreadLocalRandom that was introduced in JDK 7 for multi-threaded programs.

We can see below that we have to call the current() method of the class as we want the random numbers to be generated in the current thread.

import java.util.concurrent.ThreadLocalRandom;

public class Main {
    public static void main(String[] args) {

        int min = 1;
        int max = 10;
      
        for(int i = 1; i <=10; i++) {
        int getRandomValue = ThreadLocalRandom.current().nextInt(min, max) + min;

        System.out.println(getRandomValue);
        }
    }

}

Output:

3
4
5
8
6
2
6
10
6
2
Author: Rupam Yadav
Rupam Yadav avatar Rupam Yadav avatar

Rupam Saini is an android developer, who also works sometimes as a web developer., He likes to read books and write about various things.

LinkedIn

Related Article - Java Number