Generate a random double value between 0 and 1 in Java
This article will introduce three methods to generate double
random values between 0 and 1 of primitive types. To demonstrate the randomness of the generated values, we will use a loop to generate ten random double values between 0 and 1.
Math.random()
Generate a random double value between 0 and 1
in Java using
Math
The class can be used to perform various mathematical operations. We can also use this class to generate random numbers. Math.random()
is a static function that returns a random number between 0 and 1. Here, 0 is inclusive of the generated value and 1 is always exclusive.
In the following example, we use the Math.random()
Generate double
type to generate random values. In the output, we can see that 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
Random().nextDouble()
Generate a random double value between 0 and 1
in Java using
Another way to generate random numbers between 0 and 1 is with the RandomAccessKey nextDouble()
, which is java.util.Random
a part of the RandomAccessKey class. When we Random
call RandomAccessKey with an object of the RandomAccessKey class nextDouble()
, it returns a random value between 0 and 1, as we have seen in the previous examples.
This method is said to be Math.random()
more effective than .
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
ThreadLocalRandom.current().nextDouble()
Generate a random double value between 0 and 1
in Java using
Both of the techniques we have seen in this tutorial are not efficient for multi-threaded systems and may result in poor performance. This is because when we use Random
the class to generate random numbers, all threads share the same instance, which means that when a change is made in one thread, all other threads are also executed.
To solve this problem, Java introduced ThreadPool in JDK 7 update ThreadLocalRandom
. It is a class that runs only on the current thread, thus achieving better performance in a multi-threaded environment.
In the following example, we call nextDouble()
, which ThreadLocalRandom.current()
generates a double random value using , which returns a randomly generated 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
For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.
Related Articles
Setting the seed of a random generator in Java
Publish Date:2025/04/14 Views:62 Category:Java
-
A seed is a number or vector assigned to a pseudo-random generator to generate the desired sequence of random values. If we pass the same seed, it will generate the same sequence. We usually assign the seed as the system time. In this way,
Switching on enumeration types in Java
Publish Date:2025/04/14 Views:96 Category:Java
-
This article explains how to use enum in Java . We will use statement switch with enum in two ways . switch In Java, we use traditional switch and case to perform enumeration operations. switch In this example, we SwitchEnum create an enume
How to delay for a few seconds in Java
Publish Date:2025/04/14 Views:131 Category:Java
-
This tutorial explains how to create program delays in Java and gives some sample code to understand it. There are several ways to create a time delay, such as TimeUnit.sleep() ,, ScheduleAtFixedRate() etc. Thread.sleep() Let's look at an e
How to Convert Hashmap to JSON Object in Java
Publish Date:2025/04/14 Views:112 Category:Java
-
This article explains how to convert a Hashmap to a JSON object in Java. We will see a detailed example of creating a hashmap and then converting it to a JSON object. Hashmap and JSON are both very commonly used by developers as they help u
How to sort a Map by value in Java
Publish Date:2025/04/14 Views:171 Category:Java
-
This tutorial explains how to Mapkey, value sort pairs by value in Java and lists some sample codes to understand it. There are several ways to Map sort . Here we use sort() the , sorted() method, and Comparator interface. Let's look at an
How to Print a HashMap in Java
Publish Date:2025/04/14 Views:85 Category:Java
-
This tutorial explains how to print HashMap an element in Java and also provides some sample code to understand the topic. HashMap It is Map an implementation class of the interface and is used to collect elements into key and value pairs.
Updating Hashmap Values in Java
Publish Date:2025/04/14 Views:88 Category:Java
-
This article explains how to use the two methods contained in the HashMap class in Java - update put() and replace() update the values in a HashMap. hashmap.put() Update the value in Hashmap in Java using When we want to HashMap inser
Difference between hashmap and map in Java
Publish Date:2025/04/14 Views:193 Category:Java
-
This tutorial explains the key differences between Map and in Java HashMap . In Java, Map is an interface for storing data in key-value pairs, and HashMap is Map an implementation class of the interface. Java has several classes ( TreeHashM
Get user home directory in Java
Publish Date:2025/04/14 Views:162 Category:Java
-
This tutorial explains how to get the user's home directory in Java and lists some sample code to guide you through the topic. For a multi-user operating system, each user has a file system directory; this directory is called the user's hom