I am trying to use the following code (taken from the internet) to generate numbers from binomial distribution. It compiles but one execution it hangs. (I am using g++ on mac.)
Could someone suggest a working code to generate numbers from binomial distribution using C++ TR1 library features?
#include <tr1/random>
#include <iostream>
#include <cstdlib>
using namespace std;
using namespace std::tr1;
int main()
{
std::tr1::mt19937 eng;
eng.seed(time(NULL));
std::tr1::binomial_distribution<int, double> roll(5, 1.0/6.0);
std::cout << roll(eng) << std::endl;
return 0;
}
roll(eng)
does not seem to return. – Haematoxylon-inf
. I think it happens when-std::log(__urng())
draws a 0. The C++11 version uses slightly different code, which appears to avoid this problem, or at least avoid it almost surely. – Gaspardn
Bernoulli trials. I guess whenn
is large, that's annoying, so the GCC standard implementation uses a trick. – Gaspard