Class DiscreteDist

Class Documentation

class DiscreteDist

Distribution over a finite set of integers with specified weights.

Returns an index i in [0, n) with probability proportional to weight i. This reproduces libstdc++’s std::discrete_distribution: the weights are divided by their sum, the cumulative distribution is formed by summing them in order (with its final element then set to exactly 1), and the deviate is the index of the first element of the cumulative distribution that is not less than a uniform deviate drawn by canonical(). As in libstdc++, if there are fewer than two weights the result is always 0, and no engine output is consumed.

Public Functions

inline explicit DiscreteDist(const std::span<const double> weights)

Construct a DiscreteDist.

Parameters:

weights – The weights; these must be non-negative, and if there are two or more of them their sum must be positive and finite

Throws:

std::invalid_argument – if there are two or more weights and their sum is not positive and finite

inline auto operator()(RngType &rng) const -> std::size_t

Draw a deviate.

Parameters:

rng – The rng engine to draw from

Returns:

An index in the range [0, n), where n is the number of weights