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 between zero (inclusive) and one (exclusive). A pseudorandom double value is generated as if it's the result of calling the method nextDouble().

Syntax

public DoubleStream doubles(long streamSize)

Parameters

streamSize Specify the number of values to generate.

Return Value

Returns a stream of pseudorandom double values.

Exception

Throws IllegalArgumentException, if streamSize is less than zero.

Example:

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

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
    DoubleStream stream = rand.doubles(10);

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

One of the possible outcome is given below:

0.7021619676935855
0.5121949140779627
0.17278283521698257
0.6672681245462831
0.20313427738583256
0.9308440949098397
0.17301785765444577
0.838270346756193
0.1849557542993877
0.3970463711715079

❮ Java.util - Random