Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

java
communitycreator
long
random

Generate a finite stream of unbounded random long values

abhilash

Overview

longs is an instance method of the Random class used to generate a finite stream of random long values. The number of values to be generated is passed as an argument to the method.

The Random class is defined in the java.util package. To import the Random class, use the following import statement.

import java.util.Random;

Syntax

public LongStream longs(long streamSize)

Parameters

  • long streamSize: This is the size of the stream

Return value

The method returns a stream of random long values.

Code

import java.util.Random;

public class Main{

    public static void main(String[] args) {
        
        Random random = new Random();

        long streamSize = 10;

        random.longs(streamSize).forEach(System.out::println);

    }
}
Implementation of the longs() method

Explanation

  • Line 1: We import the Random class.

  • Line 7: We create an instance of the Random class.

  • Line 9: We define the size of the stream or the number of random values to generated.

  • Line 11: A stream of random long values are generated using the longs method passing the stream size as an argument. Next, the generated random values are printed to the console.

RELATED TAGS

java
communitycreator
long
random
RELATED COURSES

View all Courses

Keep Exploring