Java Utility Library

Java Random - doubles() Method



The java.util.Random.doubles() method returns an effectively unlimited stream of pseudorandom double values, each conforming to the given origin (inclusive) and bound (exclusive).

Syntax

public DoubleStream doubles(double randomNumberOrigin,
                            double randomNumberBound)

Parameters

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 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 double random
    //numbers between 500(inclusive) and 1000(exclusive)
    DoubleStream stream = rand.doubles(500, 1000);

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

One of the possible outcome is given below:

783.5401292652152
819.5585402522927
689.786822477947
560.32702742796
523.7952014177293
650.9592982886346
501.5919266329265
795.3955302070185
541.836358286591
963.360074768569

❮ Java.util - Random