John Bauer wrote:
I meant: if you only request one sample from the multinomial, and then instead of a vector, you get back the index that would have been set to 1.
That would in fact be equivalent to defining an arbitrary discrete distribution from a table of the probabilities of each outcome. You can do it in this way:
class CMyDistribution : public CRandomMersenne { // use any random generator as base class
private:
double const * prob; double sum; int n;
public:
void DefineProbabilities(double const * source, int colors) {
prob = source; n = colors; sum = 0;
for (int i=0; i < n; i++) sum += prob[i];}
int MakeDraw() {
double R = sum * Random() - prob[0];
int x = 0;
while (R > 0. && x < n-1) R -= prob[++x];
return x;}
};
int main() {
int seed = 123;
CMyDistribution rangen;
rangen.RandomInit(seed);
const int ncolors = 4;
static const double ProbabilityList[ncolors] = {3., 1., 4., 2.};
rangen.DefineProbabilities(ProbabilityList, ncolors);
int x = rangen.MakeDraw();
// This will give a value in the range 0-3 with probabilities 0.3, 0.1, 0.4, 0.2 respectively
printf("\nrandom draw = %i", x);
return 0;
}
|