3 ms·
C++11's random number facility isn't as good as all that. Here are some of the pain points I'm aware of: There's no way to use the random device to uniformly
by jepler 9y ago
C++11's random number facility isn't as good as all that.
Here are some of the pain points I'm aware of:
There's no way to use the random device to uniformly seed the internal state of an arbitrary pseudorandom generator. In fact, the usual thing to write will seed with only 32 bits of randomness. (this has been noted by others in this thread)
Some of the floating-point functionality such as std::uniform_real_distribution and generate_canonical have trouble where the text specifies half-open intervals like [0,1) but the (pseudo)code in practice gives closed intervals like [0,1]. Usually this can only be observed when generating values of type 'float'. This makes me generally suspect that the real number distributions in the standard library are probably poorly specified in general. (in fact, the original report that led to issue 2524 was in about the exponential distribution, which per the standard must be defined in terms of generate_canonical) http://www.open-std.org/jtc1/sc22/wg21/docs/lwg-active.html#2524 http://www.open-std.org/jtc1/sc22/wg21/docs/lwg-active.html#...
Finally, there is support for some frankly weird things in the basic random engines: you can specify a random engine that only returns the numbers 37 and 38, for instance. It turns out that e.g., std::linear_congruential_engine, including std::minstd_rand, can have a minimum possible output value of 1 rather than 0, which in turn makes the random engine's min() method necessary. But why not require engines to generate integral values on the closed interval [0..n], and make it the responsibility of linear_congruential_engine to subtract 1 before returning? A random number generator with (2^31)-2 distinct possible values is going to make for much weirder statistical anomalies than one with period 2^x when someone uses them carelessly (and what other way is there to use random numbers?).
- jepler 9y agoAnother flaw with generate_canonical<float> is that if you generate, say, 2^40 of the things, you should expect to get quite a few values smaller than 2^-32; but with minstd_rand, or mt19937 I believe you will not ever see values smaller than 2^-32 or so. A similar problem exists with double, though it takes a much longer-running problem to prove it (ain't nobody got time to generate 2^61 random numbers)