This post will explain the methods for generating random numbers within the specified range in Java.
How to Generate Random Number Within a Specified Range in Java?
For generating a random number within a specified range, you can use:
Let’s try to understand the working of these methods with examples.
Method 1: Generate a Double Random Number Within a Specified Range Using Math.random() Method
In this section, we will use the “Math.random()” method for generating random numbers. The random() method is the static method of the Math class, so it can be called directly with the class name and generates a “double” type random number between the given range.
Syntax
For generating a random number within a particular range using the Math.random() method, follow the given syntax:
The “max_num” is the maximum value while the “min_num” is the minimum value. Both values are added to provide a range.
Example
In this example, first we will create two integer type variables, “max_num” and “min_num” and assign the following values:
int max_num = 150;
We will create one more integer type variable “rand_num” that stores a random number between “0” to “136”; as using the specified formula, when we subtract min_num from the max_num, it will return 136. So, the random number will be generated between 0 to 136:
Print the generated random value using the “System.out.println()” method:
The output displays the random double value “31.592….” in between the “0” to “136”:
Now, let’s check out the second method to generate an int type random number.
Method 2: Generate an int Random Number Within a Specified Range Using Random.nextInt() Method
For generating a random integer number within a specific range, use the “nextInt()” method of the Java Random Class. The Random class provides many methods to generate random numbers in any type, such as float, double, long, and int.
In this section, we will generate an int type random number using the method of Random class.
Syntax
Follow the given syntax for generating an integer type random number using the Random class:
Here, “rand” is the object of the Random class that invokes the “nextInt()” method by passing “max_num” and “min_num” as arguments.
Example
Firstly, we will create an instance of the Random class named “rand”:
Next, create an integer type variable “x” that stores the generated random number by invoking the “nextInt()” method. This method will generate a random number between “14” to “136” because, according to the given formula, when we subtract 14 from 150, it will return 136. So, the random number will be generated between 14 to 136:
Lastly, print the generated random number on the console:
As you can see, “108” is our resultant random number:
Follow the below-given procedure if you wish to generate a random floating-point number.
Method 3: Generate a float Random Number Within a Specified Range Using Random.nextFloat() Method
“nextFloat()” is a method of the Random class that generates random numbers in floating points. In this section, we will generate a random number in decimal format with the help of the mentioned method.
Syntax
The described syntax is used for the Random.nextFloat() method:
Here, “rand” is the object of the Random class that invokes the “nextFloat()” method by passing two numbers “max_num” and “min_num” as parameters
Example
We will invoke the “nextFloat()” method by passing the already created “max_num” and “min_num” variables as arguments and storing it in the float type variable “randomVal”.
Print the random floating-point value by utilizing the “System.out.println()” method:
The output signifies that we have successfully generated a floating-point number within a specified range:
If you want to generate a list of random numbers in a specified range, follow the given section.
Method 4: Generate a List of Random Numbers Within a Specified Range Using Random.ints() Method
For generating a list of random numbers within a specific range, the “ints()” method of the Random class can be utilized. This method returns a stream of randomly generated integer numbers up to infinity. It can be used within the specified range. The ints() method also permits you to limit the number of integers by specifying the size of the stream.
Syntax
The following syntax of the “ints()” method is used to generate a list of random numbers within a specific range:
Here, “rand” is the object of the Random class that invokes the “ints()” method by passing three parameters, “max_num” and “min_num” and the limit that indicates how many random numbers should be present in the list.
Example
We will create an object “is” of the “IntStream” Interface, which will store the stream of randomly generated numbers. Here, we will pass “3” as a limit which means the resultant list will contain three numbers:
Lastly, print the randomly generated numbers by using the “forEach” loop:
Output
We have collected all the essential information for generating random numbers within a specified range in Java.
Conclusion
For generating random numbers in a specified range, Java supports different methods, Math.random() method, Random.nextInt() method, Random.nextFloat() method, and Random.ints() method. You can use these methods to generate random numbers of any type, such as double, int, float, and so on. Java also supports the generation of a list of random numbers within a specific range using the Random.ints() method. In this post, we explained the methods for generating random numbers within a specified range in Java with examples.