samedi 1 juillet 2023

std::uniform_int_distribution changed in Visual Studio 2022 version 17.6

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)?




Aucun commentaire:

Enregistrer un commentaire