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

            Bug ID: 118665
           Summary: std::uniform_int_distribution infinite loop with
                    generator returning 0
           Product: gcc
           Version: 15.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: libstdc++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: cgnitash at gmail dot com
  Target Milestone: ---

Passing a custom generator that always returns 0 to
std::uniform_int_distrubtion<int> causes it to hang (presumably due to an
infinite loop)



#include <random>

class Gen {
public:
  using result_type = std::mt19937::result_type;
  Gen() : rng(0) {}
  static constexpr result_type min() { return std::mt19937::min(); }
  static constexpr result_type max() { return std::mt19937::max(); }

  // always return 0
  result_type operator()() { return 0; }

private:
  std::mt19937 rng;
};

int main() {
    Gen g;
    std::uniform_int_distribution<int> dist(0, 42);
    return dist(g);  // hangs
}



This only happens when operator() returns exactly 0.

libc++ doesn't have this issue. 

Demo: https://godbolt.org/z/E8M5dhE59

Reply via email to