Java Random.doubles() – Examples
In this tutorial, we will learn about the Java Random.doubles() method, and learn how to use this method to generate a stream of pseudorandom double
values, with the help of examples.
doubles()
Random.doubles() returns an effectively unlimited stream of pseudorandom double values, each between zero (inclusive) and one (exclusive).
Syntax
The syntax of doubles() method is
Random.doubles()
Returns
The method returns DoubleStream object.
Example 1 – doubles()
In this example, we will generate an unlimited sequence of random double-precision floating point numbers using doubles() method and print out four of them to the console.
Java Program
import java.util.Random;
import java.util.function.DoubleConsumer;
import java.util.stream.DoubleStream;
public class Example {
public static void main(String[] args) {
Random random = new Random();
DoubleStream ds = random.doubles();
ds.limit(4).forEach(new DoubleConsumer() {
@Override
public void accept(double value) {
System.out.println(value);
}
});
}
}
Output
0.14584589858798724
0.01596337682890825
0.4461234706535546
0.5966428933676975
doubles(randomNumberOrigin, randomNumberBound)
Random.doubles() returns an effectively unlimited stream of pseudorandom double values, each conforming to the given origin (inclusive) and bound (exclusive).
Syntax
The syntax of doubles() method with random number origin and random number bound is
Random.doubles(double randomNumberOrigin, double randomNumberBound)
where
Parameter | Description |
---|---|
randomNumberOrigin | The origin (inclusive) of each random value. |
randomNumberBound | The bound (exclusive) of each random value. |
Returns
The method returns DoubleStream object.
Example 2 – doubles(randomNumberOrigin, randomNumberBound)
In this example, we will generate an unlimited sequence of random double-precision floating point numbers, which are limited by an origin and bound using doubles() method and print out four of these numbers from the stream to the console.
Java Program
import java.util.Random;
import java.util.function.DoubleConsumer;
import java.util.stream.DoubleStream;
public class Example {
public static void main(String[] args) {
double randomNumberOrigin = 1;
double randomNumberBound = 7;
Random random= new Random();
DoubleStream ds = random.doubles(randomNumberOrigin, randomNumberBound);
ds.limit(4).forEach(new DoubleConsumer() {
@Override
public void accept(double value) {
System.out.println(value);
}
});
}
}
Output
4.124126976639339
6.277075813743702
2.9896554435576643
1.6867903457923643
doubles(long streamSize)
Random.doubles() returns a stream producing the given streamSize number of pseudorandom double values, each between zero (inclusive) and one (exclusive).
Syntax
The syntax of doubles() method with stream size is
Random.doubles(long streamSize)
where
Parameter | Description |
---|---|
streamSize | The number of values to generate in the stream. |
Returns
The method returns DoubleStream object.
Example 3 – doubles(streamSize)
In this example, we will generate an specific number of random double-precision floating point numbers, using doubles(streamSize) method and print out these random numbers from the stream to the console.
Java Program
import java.util.Random;
import java.util.function.DoubleConsumer;
import java.util.stream.DoubleStream;
public class Example {
public static void main(String[] args) {
long streamSize = 5;
Random random = new Random();
DoubleStream ds = random.doubles(streamSize);
ds.forEach(new DoubleConsumer() {
@Override
public void accept(double value) {
System.out.println(value);
}
});
}
}
Output
0.24585762538765643
0.8752279248291293
0.6706517385958398
0.47129774709285743
0.8528845262464368
doubles(streamSize, randomNumberOrigin, randomNumberBound)
Random.doubles() returns a stream producing the given streamSize number of pseudorandom double values, each conforming to the given origin (inclusive) and bound (exclusive).
Syntax
The syntax of doubles() method with stream size, random number origin and random number bound is
Random.doubles(long streamSize, double randomNumberOrigin, double randomNumberBound)
where
Parameter | Description |
---|---|
streamSize | The number of values to generate in the Stream. |
randomNumberOrigin | The origin (inclusive) of each random value in the Stream. |
randomNumberBound | The bound (exclusive) of each random value in the Stream. |
Returns
The method returns DoubleStream object.
Example 4 – doubles(streamSize, randomNumberOrigin, randomNumberBound)
In this example, we will generate eight random double-precision floating point numbers which are limited by an origin and bound using doubles() method and print out these random numbers from the stream to the console.
Java Program
import java.util.Random;
import java.util.function.DoubleConsumer;
import java.util.stream.DoubleStream;
public class Example {
public static void main(String[] args) {
double randomNumberOrigin = 1;
double randomNumberBound = 7;
long streamSize = 8;
Random random = new Random();
DoubleStream ds =random.doubles(streamSize,randomNumberOrigin,randomNumberBound);
ds.forEach(new DoubleConsumer() {
@Override
public void accept(double value) {
System.out.println(value);
}
});
}
}
Output
2.392701685665302
2.9381978153219777
3.69356795462521
6.0812621302928305
5.793783278818572
5.451979737142773
1.8253898408924665
2.54037873488756
Conclusion
In this Java Tutorial, we have learnt the syntax of Java Random.doubles() method, and also learnt how to use this method with the help of Java examples.