I am trying to generate a number of series of double random numbers with high precision. For example, 0.856365621 (has 9 digits after decimal).
I've found some methods from internet, however, they do generate double random number, but the precision is not as good as I request (only 6 digits after the decimal).
Thus, may I know how to achieve my goal?
In a typical system, RAND_MAX is 231-1 or something similar to that. So your "precision" from using a method like:L
would be
1/(2<sup>31</sup)-1
- this should give you 8-9 digits "precision" in the random number. Make sure you print with high enough precision:will not do. This will work better:
Of course, there are some systems out there with much smaller RAND_MAX, in which case the results may be less "precise" - however, you should still get digits down in the 9-12 range, just that they are more likely to be "samey".
Why not create your value out of multiple calls of the random function instead?
For instance:
I would personally try a new implementation of the rand function though or at least multiply with the current time or something..
In C++11 you can using the
<random>
header and in this specific example usingstd::uniform_real_distribution
I am able to generate random numbers with more than6
digits. In order to see set the number of digits that will be printed viastd::cout
we need to usestd::setprecision
:you can use
std::numeric_limits::digits10
to determine the precision available.