Home » Java programming language

Java Random nextLong() Method with Example

Random Class nextLong() method: Here, we are going to learn about the nextLong() method of Random Class with its syntax and example.
Submitted by Preeti Jain, on March 23, 2020

Random Class nextLong() method

  • nextLong() method is available in java.util package.
  • nextLong() method is used to generate the next pseudo-random distributed long value from this Random Value Generator.
  • nextLong() 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.
  • nextLong() method does not throw an exception at the time of returning long.

Syntax:

    public long nextLong();

Parameter(s):

  • It does not accept any parameter.

Return value:

The return type of this method is long, it returns next pseudo-random distributed long value from this Random Generator.

Example:

// Java program to demonstrate the example 
// of long nextLong() method of 
// Random

import java.util.*;

public class NextLongOfRandom {
 public static void main(String args[]) {
  // Instantiates Random object
  Random ran = new Random();

  // By using nextLong() method is
  // to return next long pseudo-random
  // value by using Random Value Generator

  long val = ran.nextLong();

  // Display val
  System.out.println("ran.nextLong(): " + val);
 }
}

Output

RUN 1:
ran.nextLong(): -4596974571565686285

RUN 2:
ran.nextLong(): -9134914682951001192

RUN 3:
ran.nextLong(): -1748135394404806207


Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.