1

After upgrading to Visual Studio 2022 version 17.6, the tests of our project showed discrepancies. Performed investigation has found that the reason was in changed behavior of std::uniform_int_distribution from STL.

It can be demonstrated with the example (derived from cppreference):

#include <iostream>
#include <random>
 
int main()
{
    std::mt19937 gen(0); // mersenne_twister_engine seeded with 0
    std::uniform_int_distribution<> distrib(1, 6);
 
    // Use distrib to transform the random unsigned int
    // generated by gen into an int in [1, 6]
    for (int n = 0; n != 10; ++n)
        std::cout << distrib(gen) << ' ';
    std::cout << '\n';
}

In Visual Studio 2019 and early versions of Visual Studio 2022, it prints

3 4 6 1 2 4 2 2 2 4

And in Visual Studio 2022 version 17.6 it outputs

4 4 5 6 4 6 4 6 3 4

And the difference comes from std::uniform_int_distribution, while the behavior of std::mt19937 seems unchanged.

In Visual Studio 2022 version 17.6 Release Notes there is nothing related to this change.

My main question: is there a way to restore old behavior of std::uniform_int_distribution, e.g. by command-line switch or by some #define?

And a side question: what was the motivation of this change (e.g. the desire to make exactly the same result as in GCC, or not standard-compliance of the previous implementation)?

Fedor
  • 17,146
  • 13
  • 40
  • 131
  • 1
    In case you don't know, C++ distribution classes, such as `uniform_int_distribution`, have no standard implementation, as opposed to `mt19937` and most other C++ random engine classes: https://stackoverflow.com/questions/62538942/generate-the-same-sequence-of-random-numbers-in-c-from-a-given-seed/ – Peter O. Jul 01 '23 at 23:57
  • @PeterO., thanks. Good to know. At the same time it looks like the latest Visual Studio provide the same `uniform_int_distribution` implementation as GCC. And it is more convenient to use an implementation from standard library than to write the implementation by oneself. – Fedor Jul 02 '23 at 07:21
  • 1
    Daniel Lemire proposed fast interval sampling in https://arxiv.org/abs/1805.10941, and it was implemented first in GCC (11?) and then in Visual C++. Because uniform_int_distribution is not standartized, only generators are, there is certain freedom to change distribution algorithms – Severin Pappadeux Jul 02 '23 at 23:05
  • Thanks, @SeverinPappadeux. I see it was implemented in Microsoft STL almost a year ago [here](https://github.com/microsoft/STL/pull/3012). But only now it found its way in a public VS release. – Fedor Jul 03 '23 at 06:49

0 Answers0