mardi 8 mai 2018

C++ Custom Probability Mass Function (C++ 11)

Suppose I have a vector<double> and the sum of all entries in this vector is equal to 1. Essentially, this is a vector of probabilities.

Now suppose I want to draw some random integers with values equivalent to the index number with the corresponding probability.

For example, suppose I have vector<double> probs = {0.2, 0.5, 0.17, 0.13};

What I want to do, using this vector, is draw a random integer that takes the value 0 with 0.2 probability, 1 with 0.5 probability, 2 with 0.17 probability, and 3 with 0.13 probability, and any other value with 0 probability.

I'm not too familiar with C++11's <random>, but it seems to me as if it only has several pre-defined distributions in it; I'm not sure if there's any way I could create my own using that and then actually draw values from it. How would you achieve this in C++?

Aucun commentaire:

Enregistrer un commentaire