Home »
Java programming language
Java Random next() Method with Example
Random Class next() method: Here, we are going to learn about the next() method of Random Class with its syntax and example.
Submitted by Preeti Jain, on March 23, 2020
Random Class next() method
- next() method is available in java.util package.
- next() method is used to return the pseudo-random number in bits.
- next() method is a non-static method, it is accessible with the class object only and if we try to access the method with the class name then we will get an error.
- next() method does not throw an exception at the time of returning random bits.
Syntax:
protected int next(int ran_bit);
Parameter(s):
- int ran_bit – represents the random bits (ran_bit).
Return value:
The return type of the method is int, it returns the pseudo-random number from this Random generator.
Example:
// Java program to demonstrate the example
// of int next(int ran_bit) method of
// Random
import java.util.*;
public class NextOfRandom {
public static void main(String args[]) {
// Instantiates Random object
Random ran = new Random();
// By using next() method is
// to return the next to next
// pseudo-random number
int val = ran.nextInt();
// Display val
System.out.println("ran.nextInt(): " + val);
}
}
Output
RUN 1:
ran.nextInt(): -1105563452
RUN 2:
ran.nextInt(): -1839120956
RUN 3:
ran.nextInt(): -154633366