I want to create a Random Double with 6 decimal digits precision, buts this code doesnt do this always :/ Where is the problem of this code?
double random = Double.parseDouble(String.format("%.6f", new Random().nextDouble()));
double SP = 0.0 + (random * (7.0 - 0.0));
here is the random number that has been generated by this code for 10 times:
- 6.321637
- 6.971019999999996
- 0.763686
- 0.14724500000000001
- 0.240456
- 3.268468
- 2.112775
- 2.5137419999999997
- 4.637668
- 4.712155
Here is Full code With Correction that @javaguy Answered . And Now It Works: (But Attention To Change 'StartNumber' And 'EndNumber' like this : 2.5 )
import java.util.Random;
double random = new Random().nextDouble();
double N = SartNumber + (random * (EndNumber - StartNumber));
N = Double.parseDouble(String.format("%.6f", N)); //N Is Your Number