Random numbers from binomial distribution

I need to quickly generate many random numbers from binomial distributions for very different sample sizes (most, however, will be small). I was hoping not to code the algorithm manually (see, for example, this related discussion from November ), because I'm a newbie programmer and don't like to invent wheels. Boost doesn't seem to provide a generator for binomially distributed options, but TR1 and GSL . Is there a good reason to choose one over the other, or is it better to write something customized for my situation? I don’t know if this makes sense, but I will alternate the generation of numbers from uniform distributions and binomial distributions throughout the program, and I would like them to share the same seed and minimize overhead. I would like some tips or examples of what I should consider.

+4
source share
2 answers

Boost 1.43 seems to support binomial distributions . You can use boost::variate_generator to connect the random source to the type you want to select.

So, your code might look something like this (Disclaimer: Unverified!):

 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 
+6
source

You misunderstand the Boost model β€” you choose the type of random number generator, and then the distribution on which the values ​​generated by the RNG apply. Here's a very simple example in this answer that uses uniform distribution, but other distributions use the same basic pattern - the generator and distribution are completely decoupled.

+2
source

Source: https://habr.com/ru/post/1309147/


All Articles