Random in Java is a class of java.util package. The instance of the Java Random class generates random numbers of various types including float, double, integer, etc. This class in Java implements multiple built-in methods to provide a sequence of random numbers. Some of the built-in methods used by this class are nextBoolean(), nextDouble(), nextInt(), and nextFloat().
This article will discuss the following aspects in detail.
- The nextInt() method of the Random class in Java
- Basic Implementation of the nextInt() Method
- Implementation of the nextInt(int i) Method
- Implementing the nextInt() method using the “for loop”
- Implementing the nextInt(int i) Method using the “for loop”
- Practical Application of the Method
- Conclusion
Let us move forward and understand the nextInt() method of the Random class in Java.
The nextInt() Method of the Random Class in Java
The nextInt() method of Java’s Random class returns the next pseudorandom from the sequence of the random number generator. This method can be declared in two different ways. The first way is to declare without a parameter as the nextInt(). Another way is to declare with an integer parameter like nextInt(int i). Let us implement the two different ways in Java along with the syntax and the code for better understanding.
Example 1: Basic Implementation of the Java.util.Random.nextInt()
The syntax and the code of the nextInt() method of the Random class in Java is implemented below.
Public int nextInt()
- This method returns the next integer from the sequence of numbers.
- It does not accept any parameters.
In the code below, the nextInt() method is implemented to generate a sequence of random numbers.
//Import the package
import java.util.*;
//Declare the class
public class NextExample1 {
//main() method
public static void main(String[] args)
{ // The object to declare the random number
Random randm1 = new Random();
// generating integer
int output = randm1.nextInt();
// Print the random integer as output
System.out.println
("The Random generated Integer is: " + output);
}
}
In the above code block:
- The required package for random numbers is imported.
- The class is declared as “NextExample1” in Java.
- An object “rndm1” is created for the generation of random numbers.
- The nextInt() method gets the next integer from the random sequence of numbers using the declared object.
- A sequence of random integers is printed using the println() method of Java.
Output
A random number is generated in the output below, which may differ every time the code is executed in Java.
Example 2: Implementation of the Java.util.Random.nextInt(int i)
The nextInt() method can also be declared from the ‘0’ to any specified random number by declaring the bound value. The syntax along with the code is implemented below.
Public int nextInt(int i)
In the syntax above:
- The integer “i” is the bound for the generation of a random integer.
- It returns a random integer from 0(inclusive) and the integer bound “i” (exclusive).
Note: This method throws “IllegalArgumentException” if “i” is not declared as positive.
//Import the package
import java.util.*;
//Declare the class
public class NextExample2 {
//main() method
public static void main(String[] args)
{ // create an object of the random number
Random randm1 = new Random();
// generating an integer from 0 to 25
int output = randm1.nextInt(25);
// Print the random integer
System.out.println
("A Random generated Integer from '0' to '15' is: " + output);
}
}
In the code above:
- The class is declared as “NextExample2” in Java.
- In the nextInt() method, a bound value of “25” is declared which means that a random number is generated from 0(inclusive) to 25(exclusive).
- The println() method prints the random integer from the declared bound.
Output
The output below shows that a random number “ 2” is generated when the nextInt() method is implemented. It is important to note that the output value from the integer may differ every time the code is executed.
Example 3: Using for loop With nextInt() Method
The nextInt() method implemented in the code below is used with the “for loop” that generates a sequence of random numbers.
//Import the package
import java.util.*;
//Declare the class
public class NextExample3 {
//main() method
public static void main(String[] args)
{ // Declare an object for the random number
Random randm1 = new Random();
System.out.println("The Random Numbers are: ");
for (int x =0; x < 5; x++) {
// Print the random integer
System.out.println(randm1.nextInt());
}
}
}
In the code block:
- The required packages are imported and the class is declared as “ NextExample3” in Java.
- An object for random numbers is created as “randm1”.
- In the last part of the code, the for loop contains the condition to generate five random integers.
- The random integers are printed using the nextInt() method.
Output
The output below depicts that a total of five integers are printed according to the declared condition in the “for loop” in random order.
Example 4: Using for loop With nextInt(int i) Method
The code below implements the for loop with the nextInt(int i) method in Java that takes the bound to generate random integers.
//Import the package
import java.util.*;
//Declare the class
public class NextExample4 {
//main() method
public static void main(String[] args)
{ // declare a random number object
Random randm1 = new Random();
System.out.println("The Random Numbers are: ");
for (int x =0; x < 5; x++) {
// Print the random integer
System.out.println(randm1.nextInt(15));
}
}
}
In the above Java code:
- In the nextInt() method, the bound is declared as “15”.
- The for loop will loop through five random integer values up to the declared bound(15).
Output
The output below shows that five random integers are printed up to 15. It is to keep in mind that the integer declared as “15” in which the declared bound “15” is exclusive.
Example 5: Practical Implementation of the nextInt() Method in Java
In the code example below, the practical implementation of the nextInt() method of the Random class is discussed.
import java.util.*;
public class RandomExample5 {
public static void main(String args[])
{ // create a Random Object
Random randm = new Random();
//The player throws a dice for 3 times
for (int x = 1; x < 4; x++) {
System.out.println("Dice thrown for " + x + " time");
System.out.println ("Random number = " + (1 + randm.nextInt(6)));
}
}
}
In the above code block:
- The package is imported and the class is declared as “RandomExample5”.
- In the next step, “randm” is declared as a random object.
- The “for loop” is executed three times according to the specified condition.
- The output is printed as the number of times the dice is rolled and the second print statement prints a random number between 1 to 6 for three times the dice is rolled.
Output
The output below shows that each time the dice is thrown a random integer appears between 1 to 6.
Exception While Working with the Random nextInt() Method in Java
The exception occurs if the value entered as the bound is either “0” or “negative”. The code below shows the working of the nextInt() method which throws an exception for the declared bound value of the Random class in Java.
//Import the package
import java.util.*;
//Declare the class
public class NextExample {
//main() method
public static void main(String[] args)
{ Random randm1 = new Random();
Integer v = randm1.nextInt(0);
System.out.println("The Random number is: " + v);
}
}
In the above Java code:
- The nextInt() method of the Random class contains a value of “0”.
- The output is generated using the println() method.
Output
The output below shows that the IllegalArgumentException is raised since the bound value is declared as “0” and it must be positive and greater than “0”.
This sums up the implementation of the nextInt() method to generate random integers in Java.
Conclusion
The nextInt() method generates the next random integer value from the random number generator sequence. This method can be declared in two different ways. The first way is without the parameter whereas the second way contains an integer parameter declared as “bound”. This article has effectively elaborated the implementation of the Random.nextInt() method in Java.