Java Utility Library

Java Random - doubles() Method



The java.util.Random.doubles() method returns an effectively unlimited stream 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()

Parameters

No parameter is required.

Return Value

Returns a stream of pseudorandom double values.

Exception

NA

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 of double random numbers
    DoubleStream stream = rand.doubles();

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

One of the possible outcome is given below:

0.9199729108460707
0.28959520059126176
0.7595787933718717
0.9152088771129275
0.2091367599794457
0.1457176726162701
0.7800533655415658
0.1736180581975959
0.14473439766686325
0.49260903286863555

❮ Java.util - Random