I have encountered some odd behavior with std::binomial_distribution
when compiling with clang++
(with libstdc++
standard library).
Consider the following simple program:
#include <ctime>
#include <random>
#include <iostream>
unsigned binom(unsigned n, double p) {
std::mt19937 e(time(NULL));
std::binomial_distribution<unsigned> b(n, p);
return b(e);
}
int main() {
std::cout << "sample1=" << binom(1073741823, 0.51174692866744709) << "\n";
std::cout << "sample2=" << binom(1073741824, 0.51174692866744709) << "\n";
}
This program will output one line (sample1=511766586\n
) and then hang indefinitely.
Have I somehow invoked undefined behavior? This appears to happen regardless of what the PRNG returns. No matter how I seed it my main
hangs on this second line.