Java Utility Library

Java Random - nextLong() Method



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

Syntax

public long nextLong()

Parameters

No parameter is required.

Return Value

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

Exception

NA

Example:

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

import java.util.*;

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

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

One of the possible outcome is given below:

Next Long Value: 4825829200540759552
Next Long Value: -9066867453086882798
Next Long Value: 8469836022359840602
Next Long Value: -4746186347640095910
Next Long Value: 3109369539146836557

❮ Java.util - Random