Search code examples
c++boostrandomgamma-distribution

Gamma distributed random variables in C++


What is the easiest way to get a gamma distributed random variable in C++? Boost seems to have this functionality, but it is not clear for me how to use it.


Solution

  • It’s pretty straightforward:

    boost::mt19937 rng;
    boost::gamma_distribution<> pdf(alpha);
    boost::variate_generator<boost::mt19937&, boost::gamma_distribution<> >
        generator(rng, pdf);
    

    Constructs a random number generator and a gamma distribution and glues them together into a usable generator. Now you can create random numbers by invoking the generator.