dice

Rolls a dice with relative probabilities stored in proportions. Returns the index in proportions that was chosen.

Parameters

rnd Rng

(optional) random number generator to use; if not specified, defaults to rndGen

proportions Num[]

forward range or list of individual values whose elements correspond to the probabilities with which to choose the corresponding index value

Return Value

Type: size_t

Random variate drawn from the index values [0, ... proportions.length - 1], with the probability of getting an individual index value i being proportional to proportions[i].

Examples

auto x = dice(0.5, 0.5);   // x is 0 or 1 in equal proportions
auto y = dice(50, 50);     // y is 0 or 1 in equal proportions
auto z = dice(70, 20, 10); // z is 0 70% of the time, 1 20% of the time,
                           // and 2 10% of the time
auto rnd = MinstdRand0(42);
auto z = rnd.dice(70, 20, 10);
assert(z == 0);
z = rnd.dice(30, 20, 40, 10);
assert(z == 2);

Meta

Suggestion Box / Bug Report