How to Generate Random Double Values Between 0 and 1 in Java

Rupam Yadav Feb 02, 2024
  1. Generate Random Double Values Between 0 and 1 Using Math.random() in Java
  2. Generate Random Double Values Between 0 and 1 Using Random().nextDouble() in Java
  3. Generate Random Double Values Between 0 and 1 Using ThreadLocalRandom.current().nextDouble() in Java
How to Generate Random Double Values Between 0 and 1 in Java

This article will introduce three methods to generate random values between 0 and 1 of primitive type double. To prove the randomness of the generated values, we will use a loop to generate ten random double-type values between 0 and 1.

Generate Random Double Values Between 0 and 1 Using Math.random() in Java

The class Math can be used to perform various mathematical operations. We can use this class to generate random numbers too. Math.random() is the static function that returns random numbers between 0 and 1. Here, 0 is inclusive of the generated values, while 1 is always exclusive.

In the following example, we use Math.random() to generate random values of type double. In the output, we can see the values are all random.

public class RandomDouble {
  public static void main(String[] args) {
    for (int i = 0; i < 10; i++) {
      double randomDbl = Math.random();

      System.out.println(randomDbl);
    }
  }
}

Output:

0.9537872648347154
0.2863804438195172
0.5815339629441948
0.7734677312115609
0.021051510563543485
0.9064133490694901
0.6833468691871607
0.30655711217738246
0.2730784326888416
0.6804778782692341

Generate Random Double Values Between 0 and 1 Using Random().nextDouble() in Java

Another method that can generate random numbers between 0 and 1 is nextDouble(), a part of the java.util.Random class. When we call nextDouble() with the object of Random class, it returns a random value between 0 and 1, just like we saw in the previous example.

It is said that this method is more efficient than Math.random().

import java.util.Random;

public class RandomDouble {
  public static void main(String[] args) {
    Random randomObj = new Random();

    for (int i = 0; i < 10; i++) {
      double randomDbl = randomObj.nextDouble();

      System.out.println(randomDbl);
    }
  }
}

Output:

0.240017494934622
0.08331956619499614
0.4359524465181911
0.5291811081068774
0.38193057731688373
0.6969527822622924
0.5436002348156281
0.32176862575520415
0.07327708002828293
0.9005635171231344

Generate Random Double Values Between 0 and 1 Using ThreadLocalRandom.current().nextDouble() in Java

The two techniques that we saw in this tutorial are not efficient for multi-threaded systems and may result in poor performance. It happens because when we generate random numbers using the Random class all the threads share the same instance, which means that when a change occurs on one thread, all the other threads are also executed.

To solve this problem, Java introduced ThreadLocalRandom in the JDK 7 update. It is a class that runs only on the current thread, resulting in better performance in an environment with multiple threads.

In the below example, we call the nextDouble() that generates double random values with ThreadLocalRandom.current(), and it returns a randomly generate a double value between 0 and 1.

import java.util.concurrent.ThreadLocalRandom;

public class RandomDouble {
  public static void main(String[] args) {
    for (int i = 0; i < 10; i++) {
      double randomDbl = ThreadLocalRandom.current().nextDouble();

      System.out.println(randomDbl);
    }
  }
}

Output:

0.9717084711770174
0.8977374014983726
0.2744375247405819
0.2866498720386894
0.6118970047667582
0.7441044456568308
0.21043457873690274
0.08985457420563114
0.86748682220748
0.18952106607144148
Author: Rupam Yadav
Rupam Yadav avatar Rupam Yadav avatar

Rupam Saini is an android developer, who also works sometimes as a web developer., He likes to read books and write about various things.

LinkedIn

Related Article - Java Random