Generating Random Doubles in Java
double, java
Solution
Try this:
Random r = new Random();
double d = -10.0 + r.nextDouble() * 20.0;
Note: it should be 20.0 (not 21.0)
Problem
I'm a Java noob trying to generate a random double between -10 and 10 inclusive. I know with ints I would do the following: ``` Random r = new Random(); int i = -10 + r.nextInt(21); ``` However, with doubles, this doesn't work: ``` Random r = new Random(); double i = -10 + r.nextDouble(21); ``` Can someone please explain what to do in the case of doubles?