Java Utility Library

Java Random - doubles() Method



The java.util.Random.doubles() method returns a stream producing the given streamSize number of pseudorandom double values, each conforming to the given origin (inclusive) and bound (exclusive).

Syntax

public DoubleStream doubles(long streamSize,
                            double randomNumberOrigin,
                            double randomNumberBound)

Parameters

streamSize Specify the number of values to generate.
randomNumberOrigin Specify the origin (inclusive) of each random value.
randomNumberBound Specify the bound (exclusive) of each random value.

Return Value

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

Exception

  • Throws IllegalArgumentException, if streamSize is less than zero.
  • Throws IllegalArgumentException, if randomNumberOrigin is greater than or equal to randomNumberBound.

Example:

In the example below, the java.util.Random.doubles() method is used to get a stream of pseudorandom double values in the given range.

import java.util.*;
import java.util.stream.DoubleStream;

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

    //generating a stream containing 10 double random
    //numbers between 500(inclusive) and 1000(exclusive)
    DoubleStream stream = rand.doubles(10, 500, 1000);

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

One of the possible outcome is given below:

553.7927606329839
974.0780506574687
630.3105833454167
856.2207602933026
590.8355797491715
586.5628935251079
585.0429574635004
747.1498525516263
942.644648025693
693.4356709664102

❮ Java.util - Random