Java Random nextDouble

In this tutorial, we will see Java Random nextDouble method.It is used to generate random double. It returns the next pseudorandom, uniformly distributed double value between 0.0 and 1.0 from this random number generator’s sequence.

Syntax

`random.nextDouble()`

Here random is object of the java.util.Random class.

Return

returns random double.

Example

Let’s understand with the help of simple example

```package org.arpit.java2blog;

import java.util.Random;

public class RandomNextDoubleMain {

public static void main(String[] args) {
Random random=new Random();
System.out.println("Random Double: "+random.nextDouble());
System.out.println("Random Double: "+random.nextDouble());
System.out.println("Random Double: "+random.nextDouble());
}
}```

Output:

Random Double: 0.00885533336458566
Random Double: 0.5098436969133946
Random Double: 0.685286317774665

Generate double in the range

You can use below code to generate double in the range.

```package org.arpit.java2blog;

import java.util.Random;

public class RandomNextDoubleMain {

public static void main(String[] args) {
Random random=new Random();

int rangeMin=10;
int rangeMax=20;

System.out.println("===========================================");
System.out.println("Generating random numbers in range of 10 to 20");
System.out.println("===========================================");

for (int i = 0; i Output:

Generating random numbers in range of 10 to 20
===========================================
Generating random numbers in range of 10 to 20
===========================================
17.185913130079236
10.599693774176135
19.737211130483843
19.779771283014075
11.083440540484645
===========================================
Using ThreadLocalRandom to generate double in range of 10 to 20:
===========================================
14.490580004505432
15.959990499785127
18.44256504725257
14.841549130119944
13.015851077182596

That’s all about Java Random Nextdouble method.
The post Java Random nextDouble appeared first on Java2Blog.```

This post first appeared on How To Learn Java Programming, please read the originial post: here

Share the post

Java Random nextDouble

×