Java – Create Random Double Value
To create a random double precision number in Java, use Random.nextDouble() method. Create java.util.Random class object and call nextDouble() method on this object. nextDouble() returns a randomly generated double precision number between -1.0 and 1.0.
In the following example, we will create a random double value using nextDouble() method.
Java Program
</>
Copy
import java.util.Random;
public class Example{
public static void main(String[] args) {
Random random = new Random();
double d = random.nextDouble();
System.out.println("Next random double value is : " + d);
}
}
Output
Next random double value is : 0.2885113742924815
Reference to Random.nextDouble() method for syntax and more example programs.
Conclusion
In this Java Tutorial, we learned how to create a random double value using java.util.Random.nextDouble() method.