java.util.Random doubles(long streamSize, double randomNumberOrigin, double randomNumberBound)

Description

The doubles(long streamSize, double randomNumberOrigin, double randomNumberBound) method of Random class is an overloaded method of doubles(). This is simply the same as calling DoubleStream doubles(double randomNumberOrigin, double randomNumberBound), the only thing added is the streamsize. In practice I believe that this method is unnecessary as DoubleStream method already has a limit() method which basically function the same.

Method Syntax

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

Method Argument

Data Type Parameter Description
long streamSize the number of values to generate
double randomNumberOrigin the origin (inclusive) of each random value
double randomNumberBound the bound (exclusive) of each random value

Method Returns

The doubles(long streamSize, double randomNumberOrigin, double randomNumberBound) method of Random class returns a stream of pseudorandom double values, each with the given origin (inclusive) and bound (exclusive)

Compatibility

Java 1.8

Discussion

The method doubles(long streamSize, double randomNumberOrigin, double randomNumberBound) is an overloaded method of doubles() wherein on this method requires three input which is randomNumberOrigin, randomNumberBound and streamSize.

By calling this method a pseudorandom double value is generated as if it’s the result of calling the following method with the origin and bound:

 double nextDouble(double origin, double bound) {
   double r = nextDouble();
   r = r * (bound - origin) + origin;
   if (r >= bound) // correct for rounding
     r = Math.nextDown(bound);
   return r;
 }

Notes:

The following exception can be thrown

IllegalArgumentException – if streamSize is less than zero
IllegalArgumentException – if randomNumberOrigin is greater than or equal to randomNumberBound

Java Random doubles(long streamSize, double randomNumberOrigin, double randomNumberBound) 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(long streamSize, double randomNumberOrigin, double randomNumberBound)
 * method of Random class.
 */

public class RandomDoublesStreamSizeExample {

	public static void main(String[] args) {

		Random rand = new Random();
		DoubleStream ds = rand.doubles(5,1,10);
		ds.forEach(new DoubleConsumer() {
			@Override
			public void accept(double value) {
				System.out.println(value);

			}
		});

	}

}

On the above example, we have used a value streamSize=5, randomNumberOrigin=1, and randomNumberBound=10. So the result would be a 5 random numbers with range 1 to 10.

Sample Output

Below is the sample output when you run the above example.

Java Random doubles(streamsize, origin, bound) example output