[rand.dist.samp.discrete] (original) (raw)

A discrete_distribution random number distribution produces random integers i, , distributed according to the discrete probability function in Formula 29.19.

Unless specified otherwise, the distribution parameters are calculated as: for , in which the values , commonly known as the weights, shall be non-negative, non-NaN, and non-infinity.

Moreover, the following relation shall hold:.

namespace std { template<class IntType = int> class discrete_distribution { public: using result_type = IntType;using param_type = unspecified; discrete_distribution();template<class InputIterator> discrete_distribution(InputIterator firstW, InputIterator lastW); discrete_distribution(initializer_list<double> wl);template<class UnaryOperation> discrete_distribution(size_t nw, double xmin, double xmax, UnaryOperation fw);explicit discrete_distribution(const param_type& parm);void reset();friend bool operator==(const discrete_distribution& x, const discrete_distribution& y);template<class URBG> result_type operator()(URBG& g);template<class URBG> result_type operator()(URBG& g, const param_type& parm); vector<double> probabilities() const; param_type param() const;void param(const param_type& parm); result_type min() const; result_type max() const;template<class charT, class traits> friend basic_ostream<charT, traits>& operator<<(basic_ostream<charT, traits>& os, const discrete_distribution& x);template<class charT, class traits> friend basic_istream<charT, traits>& operator>>(basic_istream<charT, traits>& is, discrete_distribution& x);};}

Effects: Constructs a discrete_distribution object with and .

[Note 1:

Such an object will always deliver the value 0.

— _end note_]

Mandates: is_convertible_v<iterator_traits<InputIterator>​::​value_type,double> is true.

If firstW == lastW, let and .

Otherwise,[firstW, lastW) forms a sequence w of length .

Effects: Constructs a discrete_distribution object with probabilities given by the Formula 29.19.

Effects: Same as discrete_distribution(wl.begin(), wl.end()).

Mandates: is_invocable_r_v<double, UnaryOperation&, double> is true.

Preconditions: If , let , otherwise let .

The relation holds.

Effects: Constructs a discrete_distribution object with probabilities given by the formula above, using the following values: If , let .

Otherwise, let for .

Complexity: The number of invocations of fw does not exceed n.

Returns: A vector<double> whose size member returns n and whose operator[] member returns when invoked with argument k for .