I would like to generate uniform random numbers in C++ between 0 and 1, in a way which does not use the standard rand()
and srand(time(NULL))
method. The reason for this is that if I run the application more than once within the same second of my clock, the seed will be exactly the same and produce the same output.
I do not want to rely on boost or OS/compiler specifics. x86 can be assumed.
It seems as though an alternate way to do this is to use TR1 (I do not have C++11) and seeding with /dev/random
in some way?
Right now I have this, but it still uses time(NULL)
as a seed which will not work well within 1 second runs:
#include <iostream>
#include <tr1/random>
int main()
{
std::tr1::mt19937 eng;
eng.seed(time(NULL));
std::tr1::uniform_int<int> unif(1, RAND_MAX);
int u = unif(eng);
std::cout << (float)u/RAND_MAX << std::endl;
}