function r = drchrnd(A,n)
%take n samples from a dirichlet distribution with parameter A (a row vector)
%code copied from the Topic Modeling Toolbox by Mark Steyvers and Tom Griffiths (via Yi Wang); comments mine

p = length(A); %dimensionality of desired distribution
r = gamrnd(repmat(A,n,1),1); %n by p; each row is a set of gamma variables with respective shape parameters given by a and scale parameters all equal to 1
r = r ./ repmat(sum(r,2),1,p); %normalize each row; each row is now a draw from the Dirichlet distribution