A quick note on generating random numbers in C++. I will just present how to use the gsl random number generator. See full details on the GSL help page.

First one need to do the overall initialization by doing:

const gsl_rng_type * rT;
gsl_rng * rgen;
rT   = gsl_rng_default;
rgen = gsl_rng_alloc (rT);

Then here is how to use each particular random number generator:

discrete
gsl_ran_discrete_t * F;
F = gsl_ran_discrete_preproc(nx, Unif);
...
draw = gsl_ran_discrete(rgen, F);
...
gsl_ran_discrete_free(F);
binomial
// no init
...
draw = gsl_ran_binomial(rgen,pr,1);
...
// no deallocate
uniform
// no init
...
draw = gsl_rng_uniform(rgen);
...
// no deallocate
uniform discrete
// no init
...
draw = gsl_rng_uniform_int(rgen,n);
...
// no deallocate
comments powered by Disqus