Table of Contents
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
1 2 3 |
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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
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
Random Double: 0.5098436969133946
Random Double: 0.685286317774665
Generate double in the range
You can use below code to generate double in the range.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
package org.arpit.java2blog; import java.util.Random; import java.util.concurrent.ThreadLocalRandom; 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 < 5; i++) { double randomDouble = rangeMin + (rangeMax - rangeMin) * random.nextDouble(); System.out.println(randomDouble); } System.out.println("==========================================="); System.out.println("Using ThreadLocalRandom to generate double in range of 10 to 20:"); System.out.println("==========================================="); for (int i = 0; i < 5; i++) { double randomNumber = ThreadLocalRandom.current().nextDouble(rangeMin, rangeMax); System.out.println(randomNumber); } } } |
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
===========================================
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.
Was this post helpful?
Let us know if this post was helpful. Feedbacks are monitored on daily basis. Please do provide feedback as that\'s the only way to improve.