java.util.Random doubles()
Description
The doubles() method of Random class 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().
Method Syntax
public DoubleStream doubles()
Method Argument
Data Type | Parameter | Description |
---|---|---|
N/A | N/A | N/A |
Method Returns
The doubles() method of Random class returns a stream of pseudorandom double values.
Compatibility
Java 1.8
Discussion
The method double() is a convenience method of Random class. Basically it also returns the same value as nextDouble() method.
Implementation Note: This method is implemented to be equivalent to doubles(Long.MAX_VALUE).
Java Random doubles() Example
Below is a simple java example on the usage of doubles() method of Random class.
package com.javatutorialhq.java.examples; import java.util.Random; import java.util.function.DoubleConsumer; import java.util.stream.DoubleStream; /* * This example source code demonstrates the use of * doubles() method of Random class. */ public class RandomDoublesExample { public static void main(String[] args) { Random rand = new Random(); DoubleStream ds = rand.doubles(); ds.limit(10).forEach(new DoubleConsumer() { @Override public void accept(double value) { System.out.println(value); } }); } }
Basically on the above example, we put a limit on the number of values inside the Stream by calling the method limit and then by using the foreach method of DoubleStream class 10 random double values from range 0 to 1 has been printed out.