Random doubles(long) in Java

doubles(long): This method is available in java.util.Random class of Java.

Syntax:

DoubleStream java.util.Random.doubles(long streamSize)

This method takes one argument. This method returns a stream producing the given streamSize number of pseudorandom double values, each between zero(inclusive) and one (exclusive).

Parameters: One parameter is required for this method.

streamSize: the number of values to generate.

Returns: a stream of double values/.

Throws:

IllegalArgumentException - if streamSize is less than zero.

Approach 1: When no exception

Java

import java.util.Random;

public class Randomdoubles2 {
    public static void main(String[] args) {

        Random random = new Random();

        long streamSize = 10991991L;

        System.out.println(random.doubles(streamSize).count());
    }
}

Output:

10991991


Approach 2: IllegalArgumentException 

Java

import java.util.Random;

public class Randomdoubles2 {
    public static void main(String[] args) {

        Random random = new Random();

        long streamSize = -1;

        System.out.println(random.doubles(streamSize).count());
    }
}

Output:

Exception in thread "main" java.lang.IllegalArgumentException: size must be non-negative at java.base/java.util.Random.doubles(Random.java:887)


No comments:

Post a Comment