Java Utility Library

Java Random - nextInt() Method



The java.util.Random.nextInt() method returns the next pseudorandom, uniformly distributed int value from this random number generator's sequence.

Syntax

public int nextInt()

Parameters

No parameter is required.

Return Value

Returns the next pseudorandom, uniformly distributed int value from this random number generator's sequence.

Exception

NA

Example:

In the example below, the java.util.Random.nextInt() method is used to get pseudorandom number, uniformly distributed int value.

import java.util.*;

public class MyClass {
  public static void main(String[] args) {
    //creating a random object
    Random rand = new Random();

    //printing uniformly distributed int random numbers
    for(int i = 0; i < 5; i++)
      System.out.println("Next Int Value: " + rand.nextInt()); 
  }
}

One of the possible outcome is given below:

Next Int Value: -516357581
Next Int Value: 240187520
Next Int Value: 436141256
Next Int Value: -1813115768
Next Int Value: -450772196

❮ Java.util - Random