Generating Random Numbers in a Range

JavaJavaBeginner
Practice Now

Introduction

Java provides several in-built methods to generate random numbers in its Math and Random classes. However, generating random numbers in a range can be challenging. In this lab, you will learn different methods to generate random numbers in a range, which includes:

Random number generator using Math class

We can use the Math.random() method to generate a float value between 0 and 1, which is suitable for generating random numbers. Still, it is challenging to generate float values in a particular range.

In this method, we will produce a random number between min and max(inclusive of min and exclusive of max) leveraging the Math.random() method's formula.

Here is the code sample:

public static int randomNumberGenerator(int min, int max) {
    double r = Math.random();
    int randomNum = (int) (r * (max - min)) + min;
    return randomNum;
}

To use this method, use the randomNumberGenerator() method and pass the minimum and maxium range integer arguments; the method will return a random integer number between the ranges.

Here is code that uses the randomNumberGenerator() to generate two random numbers; the first should be within the range of 5 to 105 and the second within the range of 2199 to 2200.

int r1 = randomNumberGenerator(5, 105);
int r2 = randomNumberGenerator(2199, 2200);

To execute this method, run the following commands in your terminal:

$ cd project
$ touch RandomNumbers.java
// paste the code
$ javac RandomNumbers.java
$ java RandomNumbers

Random number generator using the Random class

Another way to generate random numbers within a specified range is to use the nextInt() method in the Random class.

The nextInt() method generates an integer value in the range [0, n).

public static int randomNumberGenerator(int min, int max) {
    Random r = new Random();
    int randomNum = r.nextInt(max - min) + min;
    return randomNum;
}

This method takes in the min and max parameters and returns a random integer between the specified range. In this method, nextInt() produces a random number between 0 to (max-min), and then we add the min value to get the final random number.

Here is code that uses the randomNumberGenerator() to generate two random numbers; the first should be within the range of 500 to 2000, and the second should be in the range of 500 to 501.

int randomNum1 = randomNumberGenerator(500, 2000);
int randomNum2 = randomNumberGenerator(500, 501);

To execute this method, run the following commands in your terminal:

$ cd project
$ touch RandomNumbers.java
// paste the code
$ javac RandomNumbers.java
$ java RandomNumbers

Using the ints() method of Random class

We can also use the ints() method of the Random class to generate a stream of random integers within a range.

Here is the code to generate the stream:

Random r = new Random();
IntStream stream = r.ints(5, 100, 120);

Here, the ints() method takes three parameters first the count parameter, which defines how many integers we want to generate, and the bound parameter, which defines the range. For example, in our code, the bound parameter is from 100 to 120. It is an inclusive minimum (100) and an exclusive maximum(120) parameter.

After creating this stream, we can consume it using forEach() or collect to a collection like List or array.

Here is code that uses the ints() method to generate five random integers between 150 and 2000:

int min = 150; //defining the minimum range
int max = 2000; //defining the maximum range

Random r = new Random();
IntStream stream = r.ints(5, min, max);//Generating the random stream
int randomNum = stream.findFirst().getAsInt();//Getting the first value from stream

System.out.print("The random number is: " + randomNum);//Printing the random number

To execute this method, run the following commands in your terminal:

$ cd project
$ touch RandomNumbers.java
// paste the code
$ javac RandomNumbers.java
$ java RandomNumbers

Summary

Generating random numbers in a specific range in Java is essential for several applications. In this lab, you learned several methods of generating random numbers in a range such as using Math.random(), Random.nextInt(), and Random.ints(). Each method has its pros and cons, and you should choose the one that suits your needs best.

Other Java Tutorials you may like