Java Utility Library

Java Random - longs() Method



The java.util.Random.longs() method returns an effectively unlimited stream of pseudorandom long values. A pseudorandom long value is generated as if it's the result of calling the method nextLong().

Syntax

public LongStream longs()

Parameters

No parameter is required.

Return Value

Returns a stream of pseudorandom long values.

Exception

NA

Example:

In the example below, the java.util.Random.longs() method is used to get a stream of pseudorandom long values.

import java.util.*;
import java.util.stream.LongStream;

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

    //generating a stream of long random numbers
    LongStream stream = rand.longs();

    //printing 10 random numbers from the stream
    stream.limit(10).forEach(System.out::println); 
  }
}

One of the possible outcome is given below:

-1166696981232905611
-1591414238493849592
1185092752364067807
-4716978641694621541
1728354039894099650
-8404916009908236084
8990542547882404106
4933533908656496705
4889719473874696744
-5063671713401842385

❮ Java.util - Random