Random doubles(long, double, double) in Java

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

Syntax:

DoubleStream java.util.Random.doubles(long streamSize, double randomNumberOrigin, double randomNumberBound)

This method takes three arguments. This method returns a stream producing the given streamSize number of pseudorandom double values, each conforming to the given origin(inclusive) and bound (exclusive).

Parameters: Three parameters are required for this method.

streamSize: the number of values to generate.

randomNumberOrigin: the origin (inclusive) of each random valuer.

andomNumberBound: the bound (exclusive) of each random value.

Returns: a stream of pseudorandom double values, each with the given origin (inclusive) and bound (exclusive).

Throws:

IllegalArgumentException -

1. If streamSize is less than zero.

2. If randomNumberOriginis greater than or equal to randomNumberBound.

Approach 1: When no exception

Java

import java.util.Random;

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

        Random random = new Random();

        long streamSize = 10010109L;
        double randomNumberOrigin = 10.277,
randomNumberBound = 28.199;

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

Output:

10010109


Approach 2: IllegalArgumentException 

Java

import java.util.Random;

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

        Random random = new Random();

        long streamSize = 10010109L;
        double randomNumberOrigin = 100.277,
randomNumberBound = 28.199;

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

Output:

Exception in thread "main" java.lang.IllegalArgumentException: bound must be greater than origin at java.base/java.util.Random.doubles(Random.java:947)


No comments:

Post a Comment