# Looking for C/C++ implementations of sampling from multinomial and Dirichlet distributions

I'm looking for C/C++ implementations of functions that return random variates multinomial and Dirichlet distributions. This is in the context of a calculation for posterior predictive p-values, part of which includes a MCMC step. I've been using Python, but am rewriting parts of my code in C++ for speed reasons. I've been using the implementations from numpy.random thus far.

My current options include

1. Pull the implementations from numpy.random, which are most likely written in C.

2. Use the R versions of these functions, which are conveniently available for Debian in the r-mathlib library. This library has been my default choice for such things for a long time, because the R people know their stuff when it comes to probabilistic computing.

However, I'd be open to other versions of these functions. Suggestions?

EDIT: r-mathlib doesn't appear to have a function for sampling from the Dirichet distribution, though according to Wikipedia's entry on the Dirichlet distribution I can sum Gamma variates. I wonder if that is a good way of doing this.

EDIT2: If possible, please comment on why you think the implementation you suggest is a good choice.

-
Dirichlet and multinomial random generation? Smells like LDA... –  mbq Dec 13 '11 at 8:08
@mbq: LDA? What is that? –  Faheem Mitha Dec 13 '11 at 8:16
Latent Dirichlet allocation. –  mbq Dec 13 '11 at 10:10
@mbq: Yes, something like that. In what context have you seen/used LDA? –  Faheem Mitha Dec 13 '11 at 17:10
General ML reading, to be honest -- I've never tried. BTW R's gregmisc:::rdirichet works by summing gammas, so I think you can take that it is a good idea. –  mbq Dec 14 '11 at 23:14
show 1 more comment

GSL has both gsl_ran_dirichlet and gsl_ran_multinomial for the Dirichlet and multinomial distributions respectively. See the manual for the Dirichlet distribution is here and the manual page for the multinomial distribution is here. Full documentation can be found here or online here.