7

I am trying to use Boost to generate random numbers according to the beta distribution using C++. I have seen many examples online for generating random numbers according to distributions in random.hpp (e.g. this book). However, I cannot seen to translate them to use the beta distribution found in beta.hpp.

Thanks.

2 Answers 2

14

You'll first want to draw a random number uniformly from the range (0,1). Given any distribution, you can then plug that number into the distribution's "quantile function," and the result is as if a random value was drawn from the distribution. From here:

A general method to generate random numbers from an arbitrary distribution which has a cdf without jumps is to use the inverse function to the cdf: G(y)=F^{-1}(y). If u(1), ..., u(n) are random numbers from the uniform on (0,1) distribution then G(u(1)), ..., G(u(n)) is a random sample from the distribution with cdf F(x).

So how do we get a quantile function for a beta distribution? The documentation for beta.hpp is here. You should be able to use something like this:

#include <boost/math/distributions.hpp> using namespace boost::math; double alpha, beta, randFromUnif; //parameters and the random value on (0,1) you drew beta_distribution<> dist(alpha, beta); double randFromDist = quantile(dist, randFromUnif); 
Sign up to request clarification or add additional context in comments.

1 Comment

Apparently this is a popular answer, so I'll add a caveat: For certain distributions, you may have a quantile function that is near-vertical at certain points, and in this case you may run into numerical/resolution issues in which the randFromUnif floating point number you're passing in doesn't have enough resolution to generate random numbers when passed through the quantile: you'd end up getting quantized output values. So know your problem domain and your quantile function to make sure you're aware of this!
0

According to boost's demo for the random number library Random_demo.cpp and Generating integers with different probabilities

What you should do is to use "variate_generator" class to bind your random number generator and distribution.

An example may look like

#include <iostream> #include "boost/random.hpp" int main(int argc, char *argv[]) { int seed = 2018; typedef boost::random::mt19937 RandomNumberGenerator; typedef boost::random::beta_distribution<> BetaDistribution; typedef boost::variate_generator<RandomNumberGenerator&, BetaDistribution> Generator; RandomNumberGenerator Rng(seed); BetaDistribution distribution(2,5); Generator getRandomNumber(Rng,distribution); for (int idx = 0 ; idx < 1000 ; ++idx) { std::cout << getRandomNumber() << std::endl; } return 0; } 

However, in the more recent document enter link description here, it seems that boost recommends to directly passing the generator to the distribution obejct. The result from the code below is identical.

#include <iostream> #include "boost/random.hpp" int main(int argc, char *argv[]) { int seed = 2018; typedef boost::random::mt19937 RandomNumberGenerator; typedef boost::random::beta_distribution<> BetaDistribution; RandomNumberGenerator Rng(seed); BetaDistribution distribution(2,5); for (int idx = 0 ; idx < 1000 ; ++idx) { std::cout << distribution(Rng) << std::endl; } return 0; } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.