https://gcc.gnu.org/bugzilla/show_bug.cgi?id=124256

            Bug ID: 124256
           Summary: Random number generator generates different numbers
                    for the same seed, compared to all previous GCC
                    versions
           Product: gcc
           Version: 16.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: carlosgalvezp at gmail dot com
  Target Milestone: ---

Hi,

We are bumping our GCC version from 18c32a391685256d2fbd6e3e6b2f7f93200bb912 to
716ec14c573c7ec9e3732e66c7288db8f0348154-00e3b4c8f90, and some of our tests
break.

These tests run some code that relies on a random number generator, that has a
fixed seed. We compare the result of this code against a "golden ground truth"
in our tests.

However, in the new GCC version, it appears that the same random number
generator, with the same seed, is producing different numbers. This leads to
our tests failing, since the output no longer matches the "golden ground
truth". Tests that passed with a tolerance of 10E-6 now have an error of almost
1.0 due to compounding of errors.

GCC has been generating exactly the same random numbers since at least GCC 9,
and only now in GCC 16 they started to change. Is that expected?

Here is a minimal reproducible example:

https://godbolt.org/z/xnTs8aYxb

#include <array>
#include <cassert>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <random>

int main() {
    constexpr std::seed_seq::result_type seed{0U};
    constexpr double mean{32.0};
    constexpr double std{8.0};
    constexpr int n = 10;
    constexpr double tolerance = 1E-6;

    std::minstd_rand0 random_number_generator{seed};
    std::normal_distribution<double> distribution{mean, std};

    // clang-format off
    std::array<double, n> const expected{
        31.02427373, 
        23.30545565, 
        37.47431955,
        23.3984868, 
        32.26615581,
        37.95868478, 
        32.26884898, 
        27.78690235, 
        35.70025635, 
        33.60559554
    };
    // clang-format on

    for (auto e : expected) {
        double v = distribution(random_number_generator);
        std::cout << std::setprecision(10) << "v: " << v << std::endl;
        assert(std::abs(e - v) < tolerance);
    }
}


Thanks!

Reply via email to