c++ - Generating a multinomial distribution -
i trying write/find code piece/function gives multinomial distribution given number , probability array, function:
mult(n, pp[])
for example, pp={0.3, 0.2, 0.5}, (pp can have variable sizes, 2 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 following using gsl when called repeatedly in different occasions not good. understand code piece quoted below not one, looking forward suggestions have 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; }
Comments
Post a Comment