Java Random nextDouble() Method
Example
Get a random decimal number, and check that it is at least 0.0 and less than 1.0:
import java.util.Random;
public class Main {
public static void main(String[] args) {
Random rand = new Random();
double num = rand.nextDouble();
System.out.println("Is the number at least 0.0 and less than 1.0? " + (num >= 0.0 && num < 1.0));
}
}
Definition and Usage
The nextDouble() method returns a random double value from 0.0 up to 1.0. It can return 0.0, but never 1.0.
It works like Math.random(), which also returns a double from 0.0 up to 1.0.
Tip: To get a random decimal number from 0 up to 100, multiply the result: rand.nextDouble() * 100.
Syntax
public double nextDouble()
Parameter Values
None.
Technical Details
| Returns: | A random double value, at least 0.0 and less than 1.0. |
|---|
Related Pages
Java How To Generate Random Numbers
❮ Random Methods