Draw random numbers from Boost binomial distribution

456 Views Asked by At

Here is an example to draw random numbers from a binomial distribution with std::binomial_distribution

#include <random>
int main ()
{
   std::mt19937 eng(14);
   std::binomial_distribution<size_t> dist(28,0.2);
   size_t randomNumber = dist(eng);

   return 0;
}

I am failing to find a similar example for boost. I went through this documentation, which explains how to compute PDF, CDF and others from a boost::math::binomial object but they are not talking about sampling a random number.

Should I write a binary search myself based on the CDF that boost::math::binomial will compute for me or can boost directly return random numbers?

1

There are 1 best solutions below

1
On

Thanks to this link from @Bob__, here is a simple working example

#include <random>
#include <boost/random.hpp>

int main ()
{
   std::mt19937 eng;
   boost::random::binomial_distribution<int> dist(28,0.2);
   int randomNumber = dist(eng);

   return 0;
}

For some reason, it would not compile with size_t, so I used int (see @Bob__'s comment below for more information).