I am trying to write/find a code piece/function which gives back a multinomial distribution given a number and a probability array, say a function:
Mult(N, pp[])
For example, for pp={0.3, 0.2, 0.5}, (pp can have variable sizes, say 2 to 10 elements).
Mult(10, pp[])
giving outputs like:
1,1,8
3,1,6
2,3,5
when called in different occasions.
I can think of something like the following using gsl but when called repeatedly in different occasions this is not good. I understand that the code piece I quoted below is not a good one, but looking forward for suggestions to have a better one. Thanks.
std::vector<int> Mult(int numb, std::vector<double> prob_array)
{
const gsl_rng_type * T2;
gsl_rng * r2;
srand(); // srand(time(NULL));
unsigned int Seed2 = 1234567; // rand();
gsl_rng_env_setup();
T2 = gsl_rng_default;
r2 = gsl_rng_alloc (T2);
gsl_rng_set (r2, Seed2);
size_t k = prob_array.size();
double ppp[k]; // Probability array
for(int ii=0; ii<prob_array.size(); ++ii) {
ppp[ii] = prob_array[ii];
}
unsigned int mult_op[k];
gsl_ran_multinomial(r2, k, numb, ppp, mult_op);
std::vector<int> multi;
for(int ii=0; ii<kk; ++ii ){
multi.push_back(mult_op[ii]);
}
return multi;
}