I need to generate quickly lots of random numbers from binomial distributions for dramatically different trial sizes (most, however, will be small). I was hoping not to have to code an algorithm by hand (see, e.g., this related discussion from November), because I'm a novice programmer and don't like reinventing wheels. It appears Boost does not supply a generator for binomially distributed variates, but TR1 and GSL do. Is there a good reason to choose one over the other, or is it better that I write something customized to my situation? I don't know if this makes sense, but I'll alternate between generating numbers from uniform distributions and binomial distributions throughout the program, and I'd like for them to share the same seed and to minimize overhead. I'd love some advice or examples for what I should be considering.
Asked
Active
Viewed 4,149 times
4
-
See [Biassed Random Number Generator](http://stackoverflow.com/questions/2775578/biased-random-number-generator/) for some discussion of non-uniform PRNGs. It doesn't directly answer your question, though, and your 'November' reference looks relevant too. – Jonathan Leffler May 07 '10 at 20:42
-
The "Biassed..." link refers again to the Boost libraries, which do not appear to have a PRNG for binomial distributions. The November reference gives a few approximations. Coming up with efficient and high-quality PRNGs from binomial distributions where the number of trials can vary enormously is, from what I understand, something of a mathematical challenge. I was hoping a library already exists that can handle all these scenarios. It looks like I might have to test out TR1 and GSL myself--was hoping others here might already have experience w/ them. – Sarah May 07 '10 at 20:45
2 Answers
6
Boost 1.43 appears to support binomial distributions. You can use boost::variate_generator
to connect your source of randomness to the type
of distribution you want to sample from.
So your code might look something like this (Disclaimer: not tested!):
boost::mt19937 rng; // produces randomness out of thin air
// see pseudo-random number generators
const int n = 20;
const double p = 0.5;
boost::binomial<> my_binomial(n,p); // binomial distribution with n=20, p=0.5
// see random number distributions
boost::variate_generator<boost::mt19937&, boost::binomial<> >
next_value(rng, my_binomial); // glues randomness with mapping
int x = next_value(); // simulate flipping a fair coin 20 times

Jim Lewis
- 43,505
- 7
- 82
- 96
-
If I understand it correctly, it does not actually support PRNGs from those distributions. It calculates probability and cumulative mass functions and the like. – Sarah May 07 '10 at 20:55
-
2
You misunderstand the Boost model - you choose a random number generator type and then a distribution on which to spread the values the RNG produces over. There's a very simple example in this answer, which uses a uniform distribution, but other distributions use the same basic pattern - the generator and the distribution are completely decoupled.

Community
- 1
- 1
-
Heh, I'm so not quick enough for Boost. Thanks for explaining what it's doing. – Sarah May 07 '10 at 21:57