java Random nextLong() Method



Description

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

Declaration

Following is the declaration for java.util.Random.nextLong() method.

public long nextLong()

Parameters

NA

Return Value

The method call returns the next pseudorandom, uniformly distributed long value from this random number generator's sequence.

Exception

NA

Getting a Random Long Value Example

The following example shows the usage of Java Random nextLong() method. Firstly, we've created a Random object and then using nextLong() we retrieved a random long value and printed it.

package com.tutorialspoint;

import java.util.Random;

public class RandomDemo {
   public static void main( String args[] ) {
      
      // create random object
      Random randomNo = new Random();

      // get next long value 
      long value = randomNo.nextLong();

      // check the value  
      System.out.println("Long value is: " + value);
   }    
}

Output

Let us compile and run the above program, this will produce the following result.

Long value is: -2584450756196554194

Getting a Random Long Value With Given Seed Example

The following example shows the usage of Java Random nextLong() method. Firstly, we've created a Random object with a given seed value and then using nextLong() we retrieved a random long value and printed it.

package com.tutorialspoint;

import java.util.Random;

public class RandomDemo {
   public static void main( String args[] ) {
      
      // create random object
      Random randomNo = new Random(10);

      // get next long value 
      long value = randomNo.nextLong();

      // check the value  
      System.out.println("Long value is: " + value);
   }    
}

Output

Let us compile and run the above program, this will produce the following result.

Long value is: -4972683369271453960
java_util_random.htm
Advertisements