What is the Random.doubles method in Java?

Overview

doubles is an instance method of the Random class used to generate a stream of random double values. There are four different variants of the method:

  • doubles(long streamSize)
  • doubles()
  • doubles(long streamSize, double randomNumberOrigin, double randomNumberBound)
  • doubles(double randomNumberOrigin, double randomNumberBound)

The Random class is defined in java.util package. To import the Random class, use the following import statement:

import java.util.Random;

doubles(long streamSize)

This method generates a stream of random double values of the specified size:

public DoubleStream doubles(long streamSize)

Parameters

  • long streamSize: The size of the stream.

Return value

The method returns a stream of random double values.

Code

import java.util.Random;
public class Main{
public static void main(String[] args) {
//create an object of the Random class
Random random=new Random();
// stream size
long streamSize = 10;
// using doubles generate stream of random doubles
random.doubles(streamSize).forEach(System.out::println);
}
}

doubles()

This method generates an infinite stream of random double values.

public DoubleStream doubles()

Parameters

The method has no parameters.

Return value

The method returns a stream of random double values.

Code

import java.util.Random;
public class Main{
public static void main(String[] args) {
//create an object of the Random class
Random random=new Random();
// using doubles generate stream of random doubles
random.doubles().limit(4).forEach(System.out::println);
}
}

doubles(long streamSize, double randomNumberOrigin, double randomNumberBound)

This method generates a stream of pseudorandom double values of the provided size, each adhering to the supplied origin (inclusive) and bound (exclusive).

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

Parameters

  • long streamSize: The size of the stream.
  • double randomNumberOrigin: The origin of each value generated.
  • double randomNumberBound: The bound of each value generated.

Return value

The method returns a stream of random double values.

Code

import java.util.Random;
import java.util.stream.DoubleStream;
public class Main{
public static void main(String[] args) {
//create an object of the Random class
Random random=new Random();
// size of the stream
int streamSize = 8;
// origin of each random number
double randomNumberOrigin = 1.4;
// bound of each random number
double randomNumberBound = 7.53;
// Stream of random doubles generated
DoubleStream ds = random.doubles(streamSize, randomNumberOrigin, randomNumberBound);
// print the generated random values
ds.forEach(System.out::println);
}
}

doubles(double randomNumberOrigin, double randomNumberBound)

This method generates an infinite stream of pseudorandom double values, each adhering to the supplied origin (inclusive) and bound (exclusive).

public DoubleStream doubles(double randomNumberOrigin, double randomNumberBound)

Parameters

  • double randomNumberOrigin: The origin of each value generated.
  • double randomNumberBound: The bound of each value generated.

Return value

The method returns a stream of random double values.

Code

import java.util.Random;
import java.util.stream.DoubleStream;
public class Main{
public static void main(String[] args) {
//create an object of the Random class
Random random=new Random();
// origin of each random number
double randomNumberOrigin = 1.42;
// bound of each random number
double randomNumberBound = 7.23;
// Stream of random doubles generated
DoubleStream ds = random.doubles(randomNumberOrigin, randomNumberBound);
// print the generated random values
ds.limit(10).forEach(System.out::println);
}
}