Java Utility Library

Java Random - longs() Method



The java.util.Random.longs() method returns a stream producing the given streamSize number 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(long streamSize)

Parameters

streamSize Specify the number of values to generate.

Return Value

Returns a stream of pseudorandom long values.

Exception

Throws IllegalArgumentException, if streamSize is less than zero.

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 containing 10
    //long random numbers
    LongStream stream = rand.longs(10);

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

One of the possible outcome is given below:

5360782108241675042
-4693184344374823843
7900676258285631904
4044430131010126681
3164930263573636076
6708605751717819696
-6449429044332939763
1583025717856756815
-1463454922235472079
-2643849658836944600

❮ Java.util - Random