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

Description

The doubles(double randomNumberOrigin, double randomNumberBound) method of Random class is an overloaded method of doubles(). Basically this method returns an effectively unlimited stream of pseudorandom double values, each conforming to the given origin (inclusive) and bound (exclusive).

Method Syntax

public DoubleStream doubles(double randomNumberOrigin, double randomNumberBound)

Method Argument

Data Type Parameter Description
double randomNumberOrigin the origin (inclusive) of each random value
double randomNumberBound the bound (exclusive) of each random value

Method Returns

The doubles() 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(double randomNumberOrigin, double randomNumberBound) is an overloaded method of doubles() wherein on this method requires two input which is randomNumberOrigin and randomNumberBound.

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;
 }

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 
 * nextDouble(double origin, double bound)) method of Random class.
 */

public class RandomDoublesOriginExample {

	public static void main(String[] args) {

		Random rand = new Random();
		DoubleStream ds = rand.doubles(5,10);
		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 6 to 10 has been printed out.

Sample Output

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

Java Random doubles(origin, bound) example output