How do you generate a random double uniformly distributed between 0 and 1 from C++?
Of course I can think of some answers, but I\'d like to know what the standard pr
In C++11 and C++14 we have much better options with the random header. The presentation rand() Considered Harmful by Stephan T. Lavavej explains why we should eschew the use of rand()
in C++ in favor of the random
header and N3924: Discouraging rand() in C++14 further reinforces this point.
The example below is a modified version of the sample code on the cppreference site and uses the std::mersenne_twister_engine engine and the std::uniform_real_distribution which generates numbers in the [0,1)
range (see it live):
#include <iostream>
#include <iomanip>
#include <map>
#include <random>
int main()
{
std::random_device rd;
std::mt19937 e2(rd());
std::uniform_real_distribution<> dist(0, 1);
std::map<int, int> hist;
for (int n = 0; n < 10000; ++n) {
++hist[std::round(dist(e2))];
}
for (auto p : hist) {
std::cout << std::fixed << std::setprecision(1) << std::setw(2)
<< p.first << ' ' << std::string(p.second/200, '*') << '\n';
}
}
output will be similar to the following:
0 ************************
1 *************************
Since the post mentioned that speed was important then we should consider the cppreference section that describes the different random number engines (emphasis mine):
The choice of which engine to use involves a number of tradeoffs*: the **linear congruential engine is moderately fast and has a very small storage requirement for state. The lagged Fibonacci generators are very fast even on processors without advanced arithmetic instruction sets, at the expense of greater state storage and sometimes less desirable spectral characteristics. The Mersenne twister is slower and has greater state storage requirements but with the right parameters has the longest non-repeating sequence with the most desirable spectral characteristics (for a given definition of desirable).
So if there is a desire for a faster generator perhaps ranlux24_base or ranlux48_base are better choices over mt19937.
rand()
If you forced to use rand()
then the C FAQ for a guide on How can I generate floating-point random numbers?, gives us an example similar to this for generating an on the interval [0,1)
:
#include <stdlib.h>
double randZeroToOne()
{
return rand() / (RAND_MAX + 1.);
}
and to generate a random number in the range from [M,N)
:
double randMToN(double M, double N)
{
return M + (rand() / ( RAND_MAX / (N-M) ) ) ;
}
First include stdlib.h
#include<stdlib.h>
Then following can be a function to generate random double number between a range in C programming language.
double randomDouble() {
double lowerRange = 1.0;
double upperRange = 10.0;
return ((double)rand() * (upperRange - lowerRange)) / (double)RAND_MAX + lowerRange;
}
Here RAND_MAX is defined in stdlib.h
This is what I ended up using for my needs:
int range_upper_bound = 12345;
int random_number =((double)rand()/(double)range_upper_bound);
An old school solution like:
double X=((double)rand()/(double)RAND_MAX);
Should meet all your criteria (portable, standard and fast). obviously the random number generated has to be seeded the standard procedure is something like:
srand((unsigned)time(NULL));
You could try the Mersenne Twister algorithm.
http://en.wikipedia.org/wiki/Mersenne_twister
It has a good blend of speed and randomness, and a GPL implementation.
If speed is your primary concern, then I'd simply go with
double r = (double)rand() / (double)RAND_MAX;