I was rewriting this function:
long rng(long low, long high)
{
return low + long((high - low + 1) * double(rand() / double(RAND_MAX + 1.0)));
}
Updating it to C++11 and Mersenne Twister.
long rng(long low, long high)
{
static std::mt19937 rnumber; // Mersenne Twister
std::uniform_int_distribution<unsigned> u (low, high);
return u(rnumber);
}
Now this seems to work, but my C++11 book states that in similar cases BOTH the engine and the distributor should be "static". The problem is that I want to call the function with different ranges, and if I make the distributor static then the range is always the same.
So how should this be handled in a proper way?
Two other questions:
Why Stroustrup book seems to suggest I should use {low, high} instead of (low, high)? What's the difference?
Is is plausible that with gcc 4.8.1 and no optimization rand is twice as fast as Mersenne, whereas with -O3 rand has the same performance, while Mersenne becomes even actually faster than rand?