
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Generate Random Integers Within a Specific Range in Java
Suppose we are in a situation where we need to generate random integer numbers within a specific range through Java programs. For the given scenario, there are two distinct ways available in Java. We can use either the Random class or random() method. Let's discuss them in the next section.
Generate Random Integers within a Specific Range
We are going to use the following class and method ?
Random Class
We create an object of this class to return pseudorandom numbers within a given range.We will customize this object and apply our own logic to generate any random valueswithin the specified range. To retrieve the values within a range, we need a built-in methodnamed ?nextInt()' that returns the next integer value from the specified sequence. It isused along with the object of Random class.
Following is the syntax to create an object of class Random ?
Syntax
Random nameOfObject = new Random();
Math.random()
It is a static method of Math class that is used to return pseudorandom numbers of type double. Generally, it generates a random number between 0.0 to 1.0 however, we will use it with our logic to generate integer numbers.
Approach 1
Create a user-defined method along with two parameters of type integer. The idea is to generate a random number between these two parameters.
Now, define an object of class Random and using the built-in method ?nextInt()' along with the object, generate a random integer
In the main() method, call that user-defined method we have created in the first step with two arguments that denotes the range.
Example
The following example illustrates the use of Random class to generate a random integer number in a given range.
import java.util.*; public class Randomly { public static void generator(int start, int end) { Random rndm = new Random(); // object of class Random int val = rndm.nextInt(end - start) + start; System.out.println("A random value between given range: " + val); } public static void main(String[] args) { int start = 5; int end = 10; generator(start, end); // calling the method with arguments } }
Output
A random value between given range: 5
Approach 2
First, define a range of type integer.
Take a for loop that will run till the specified range and produce multiple random numbers.
Since ?Math.random()' returns a double value, therefore, we need to typecast it into an integer.
Example
In the following example, we will generate multiple random numbers using Math.random().
public class Randomly { public static void main(String[] args) { int range = 5; // given range for(int i = 1; i <= range; i++) { double db1 = Math.random() + i; int val = (int) db1; // typecasting to integer System.out.println("Values at " + i + " : " + val); } } }
Output
Values at 1 : 1 Values at 2 : 2 Values at 3 : 3 Values at 4 : 4 Values at 5 : 5
Approach 3
Create a user-defined method along with two parameters of type integer that define the range
Now, using ?Math.random()' generate a random value and typecast it to an integer and then, print it.
Example
The following example demonstrates the use of Math.random() to generate a random integer number in a given range.
import java.util.*; public class Randomly { public static void generator(int start, int end) { double val1 = Math.random() * (end - start) + start; int val2 = (int) val1; // typecasting System.out.println("A random value between given range: " + val2); } public static void main(String[] args) { int start = 5; int end = 10; generator(start, end); // calling the method } }
Output
A random value between given range: 5
Conclusion
In this article, we have discussed a few Java programs to generate a random integer in a specific range. During this, we discovered the use of the Random class and Math.random() method. Both return pseudorandom numbers.