


The std::uniform_int_distribution class template produces incorrect results when the min0 parameter passed to its constructor is negative. Consider the following code snippet, which is supposed to generate a million random integers in the range 2 through 2: std::uniform_int_distribution<int> dist(2, 2); std::default_random_engine rng; int n[5] = { 0 }; for (int i = 1000000; i > 0; i) { ++n[dist(rng) + 2]; } std::cout << n[0] << ", " << n[1] << ", " << n[2] << ", " << n[3] << ", " << n[4] << std::endl; The expected output of this code would be something like: 199098, 200508, 200762, 199867, 199765 Note the roughly uniform distribution among the valid output values. Under Visual Studio 2010 SP1, we get this instead: 0, 199098, 401270, 199867, 199765 This appears to be caused by a rounding error in line 2788 of random.h, which does a simple cast of the internal floatingpoint result to the specified result type. Consequently, all of the 1 values are returned as 0, and all of the 2 values are returned as 1. Observe that 0 is returned twice as many times as expected, and 2 is not returned at all. In the above example, the problem can be worked around by specifying 0 and 4 as min0 and max0, respectively, and then subtracting 2 from the return value. This is ugly, however, and shouldn't be necessary.
Thanks for reporting this bug. We've already fixed it, and the fix is available in the VC11 Developer Preview.
(I rewrote uniform_int_distribution to use bitwise operations instead of floatingpoint arithmetic; it now produces perfectly uniform results for arbitrary input ranges and arbitrary output ranges.)
If you have any further questions, feel free to Email me at stl@microsoft.com .
Stephan T. Lavavej
Visual C++ Libraries Developer