Java Utility Library

Java Random - next() Method



The java.util.Random.next() method is used to generate the next pseudorandom number.

Syntax

protected int next(int bits)

Parameters

bits Specify the random bits.

Return Value

Returns the next pseudorandom value from this random number generator's sequence.

Exception

NA

Example:

In the example below, the java.util.Random.next() method is used to generate the next pseudorandom number.

import java.util.*;

// extending the class to random because 
// next() is a protected method 
public class MyClass extends Random {
  public static void main(String[] args) {
    //creating a random object
    MyClass rand = new MyClass();

    //printing random numbers using next method
    for(int i = 0; i < 5; i++)
      System.out.println("Next Int Value: " + rand.next(10)); 
  }
}

One of the possible outcome is given below:

Next Int Value: 429
Next Int Value: 554
Next Int Value: 43
Next Int Value: 429
Next Int Value: 130

❮ Java.util - Random