# How to Generate random numbers in a specific range in Java

In this tutorial, we are going to learn how to generate random numbers in a specific range in Java. To do this we can use different methods like Math.random, random class, or ThreadLocalRandom.

## Generating random numbers in a specific range in Java

Now, let us look at the different ways of getting random numbers in a specific range.

#### Using Math.random()

Math.random() method will give us a pseudorandom number of type double that is greater than or equal to 0.0 and less than 1.0. Hence to get a random number between a specific range we modify it as shown below.

//This will give us a number between 0 and Num rand = Math.random() * Num;

//Now to get a number between a range we use: rand = Math.random()*(maximum-minimum+1)+minimum;

The following program demonstrates the above concept and generates a random integer between the specified minimum and maximum values. we use the floor function to round off to the integer value.

public class MathRand { public static void main(String args[]) { int minimum = 1, maximum = 500; int randNum = (int)Math.floor(Math.random()*(maximum-minimum+1)+minimum); System.out.println("Random number between "+minimum+" and "+maximum+" is: "+ randNum); } }

**Output:**

A random number between 1 and 500 is: 204

A random number between 1 and 500 is: 259

A random number between 1 and 500 is: 24

#### Using the random class

Next, we can generate a random number using a random class. This class will return a pseudo-random number. This class has various methods to generate random numbers of different data types.

nextInt(maximum) - This method will generate an integer from the range 0 to maximum-1.

nextDouble() - This method will generate a double between 0.0 and 1.0.

nextFloat() - This method will generate a float between o.o and 0.1.

The following program demonstrates the above-mentioned methods.

import java.util.Random; public class RandNums { public static void main(String args[]) { Random r = new Random(); int max = 100; //Generating a random integer int randInt = r.nextInt(max); System.out.println("A random integer value between 0 and "+max+" :"+randInt); //Generating a random double double randDouble = r.nextDouble(); System.out.println("A random double value between 0 and "+max+" :"+randDouble); //Generating a random float float randFloat = r.nextFloat(); System.out.println("A random float value between 0 and "+max+" :"+randFloat); } }

**Output:**

A random integer value between 0 and 100 : 52 A random double value between 0 and 100 : 0.39645215529321143 A random float value between 0 and 100 : 0.77473766

A random integer value between 0 and 100 : 64 A random double value between 0 and 100 : 0.5587072994026399 A random float value between 0 and 100 : 0.78726876

#### Using ThreadLocalRandom

This class is used to generate random numbers. This class also has different methods to generate random numbers of different data types. The syntax is given below to generate an integer between the given maximum and minimum values.

randInt = ThreadLocalRandom.current().nextInt(minimum,maximum);

we can use the method nextDouble() to generate a random double and the method nextFloat() to generate a random float. Here is a program demonstrating how to generate a random integer.

import java.util.concurrent.ThreadLocalRandom; public class TrdRand { public static void main(String args[]) { int min = 1, max = 100; //generating a random integer between min and max int randInt = ThreadLocalRandom.current().nextInt(min,max); System.out.println("A random integer between "+min+" and "+max+" is: "+randInt); } }

**Output:**

A random integer between 1 and 100 is: 92

A random integer between 1 and 100 is: 82

A random integer between 1 and 100 is: 43

Using these different ways we can generate random numbers in a specific range.

Here is a tutorial that you can check out next: How to generate random numbers in Java.

## Leave a Reply