Signup/Sign In

Generating Random Numbers in a Range in Java

Java provides a few methods to generate random numbers, but it is quite tricky to generate a random number in a specific range. In this tutorial, we will learn how to leverage the inbuilt Java methods to implement a random number generator method for a given range.

Using Math.random() Method

The random() method of the Math class is used to generate a decimal value between 0 and 1(0 inclusive, 1 exclusive).

public class RandomRangeDemo
{
	public static void main(String[] args)
	{
		double r1 = Math.random();
		double r2 = Math.random();
		System.out.println("The first random number is: " + r1);
		System.out.println("The second random number is: " + r2);
	}
}


The first random number is: 0.8998047971282644
The second random number is: 0.9465494601371991

We can use this method to build our own random method which will take the minimum and the maximum range values and it will return a random number within that range.

  • We will use the formula (Math.random() * (max-min)) + min in our method.
  • This formula works because if Math.random() generates 0(the lowest value), then (0 * (max-min)) + min will be equal to min.
  • And if Math.random() generates 1(the highest value), then the formula will give 1 * (max-min) + min, which is equal to max.
  • But remember that Math.random() does not returns 1, but instead, a value that is slightly less than 1(say 0.999999). So the maximum returned value of our function will be slightly less than max.
  • So, our method returns a value between min and max(min inclusive, max exclusive)
public class RandomRangeDemo
{
	public static int randomNumberGenerator(int min, int max)
	{
		double r = Math.random();
		int randomNum = (int)(r * (max - min)) + min;
		return randomNum;
	}
	public static void main(String[] args)
	{
		int r1 = randomNumberGenerator(5, 105);//random number between 5 and 105
		int r2 = randomNumberGenerator(2199, 2200);//random number between 2199 and 2200
		System.out.println("The first random number is: " + r1);
		System.out.println("The second random number is: " + r2);
	}
}


The first random number is: 47
The second random number is: 2199

We can also use the nextDouble() and nextFloat() methods of the Random class in place of the Math.random() method. All these methods will generate a random decimal number between 0 and 1.

The above code using the nextDouble() method is shown below.

import java.util.Random;

public class RandomRangeDemo
{
	public static int randomNumberGenerator(int min, int max)
	{
		Random r = new Random();
		double randomNum = r.nextDouble();
		int result = (int)(randomNum * (max - min)) + min;
		return result;
	}
	public static void main(String[] args)
	{
		int r1 = randomNumberGenerator(5, 105);//random number between 5 and 105
		int r2 = randomNumberGenerator(2199, 2200);//random number between 2199 and 2200
		System.out.println("The first random number is: " + r1);
		System.out.println("The second random number is: " + r2);
	}

}


The first random number is: 53
The second random number is: 2199

Using nextInt() method of Random class

The nextInt() method can be used to generate a random integer. If we pass a positive parameter n to this method then it will return a random number in the range 0 to n(0 inclusive, n exclusive).

import java.util.Random;

public class RandomRangeDemo
{
	public static void main(String[] args)
	{
		Random r = new Random();
		int randomNum1 = r.nextInt(100);
		int randomNum2 = r.nextInt(1);
		System.out.println("The first random number is: " + randomNum1);
		System.out.println("The second random number is: " + randomNum2);
	}

}


The first random number is: 63
The second random number is: 0

We will use this method to build our own random number generator method. We will use the formula nextInt(max-min) + min. Let's try to understand this formula.

For example, if max = 31 and min = 7, then (max-min) will be equal to 24. So, nextInt(24) will generate a random number between 0 and 24. If it generates 0(the lowest value), then 0 + min will be equal to 7. If it generates 24(the largest value), then 24 + min will be equal to 31, which is the max value. Since nextInt(n) is exclusive of the n value, so our method will also be exclusive of the max value.

import java.util.Random;

public class RandomRangeDemo
{
	public static int randomNumberGenerator(int min, int max)
	{
		Random r = new Random();
		int randomNum = r.nextInt(max - min);
		int result = randomNum + min;
		return result;
	}
	
	public static void main(String[] args)
	{
		int randomNum1 = randomNumberGenerator(500, 2000);//random number between 500 and 2000
		int randomNum2 = randomNumberGenerator(500, 501);//random number between 500 and 501
		System.out.println("The first random number is: " + randomNum1);
		System.out.println("The second random number is: " + randomNum2);
	}

}


The first random number is: 846
The second random number is: 500

Using ints() method of Random Class

The java.util.Random.ints() method can return an infinite integer stream of randomly generated numbers. We can specify a range and all the random numbers will be generated within that range. We can also specify the stream size so that we get only a limited number of integers.

The following code runs infinitely as we have not specified a stream size to the ints() method. We have only shown a few of the numbers in the output.

import java.util.Random;
import java.util.stream.IntStream;

public class RandomRangeDemo
{	
	public static void main(String[] args)
	{
		Random r = new Random();
		IntStream stream = r.ints(10, 20);
		stream.forEach(s -> System.out.println(s));
	}

}


11
16
16
14
12
13

Let's now generate just 5 random integers by setting the stream size.

import java.util.Random;
import java.util.stream.IntStream;

public class RandomRangeDemo
{	
	public static void main(String[] args)
	{
		Random r = new Random();
		IntStream stream = r.ints(5, 100, 120);
		stream.forEach(s -> System.out.println(s));
	}

}


103
106
114
117
109

We can use the findFirst() and getAsInt() methods to get only the first random number from the stream.

import java.util.Random;
import java.util.stream.IntStream;

public class RandomRangeDemo
{	
	public static void main(String[] args)
	{
		int min = 150, max = 2000;//defining the range
		
		Random r = new Random();
		IntStream stream = r.ints(1, min, max);
		int randomNum = stream.findFirst().getAsInt();
		
		System.out.print("The random number is: " + randomNum);
	}

}


The random number is: 1193

Summary

In this tutorial, we learned the different ways of generating a random number in a specified range. We implemented our methods using the inbuilt random() method of the Math class and the nextInt() of the Random class. We can also ints() method directly to generate a stream of random numbers in a given range.



About the author:
I am a 3rd-year Computer Science Engineering student at Vellore Institute of Technology. I like to play around with new technologies and love to code.