29 Numerics library [numerics]

29.5 Random number generation [rand]

29.5.9 Random number distribution class templates [rand.dist]

29.5.9.6 Sampling distributions [rand.dist.samp]

29.5.9.6.1 Class template discrete_distribution [rand.dist.samp.discrete]

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: // types using result_type = IntType; using param_type = unspecified; // constructor and reset functions 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(); // equality operators friend bool operator==(const discrete_distribution& x, const discrete_distribution& y); // generating functions template<class URBG> result_type operator()(URBG& g); template<class URBG> result_type operator()(URBG& g, const param_type& parm); // property functions vector<double> probabilities() const; param_type param() const; void param(const param_type& parm); result_type min() const; result_type max() const; // inserters and extractors 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); }; }
discrete_distribution();
Effects: Constructs a discrete_distribution object with and .
[Note 1: 
Such an object will always deliver the value 0.
— end note]
template<class InputIterator> discrete_distribution(InputIterator firstW, InputIterator lastW);
Mandates: is_convertible_v<iterator_traits<InputIterator>​::​value_type, double> is true.
Preconditions: InputIterator meets the Cpp17InputIterator requirements ([input.iterators]).
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.
discrete_distribution(initializer_list<double> wl);
Effects: Same as discrete_distribution(wl.begin(), wl.end()).
template<class UnaryOperation> discrete_distribution(size_t nw, double xmin, double xmax, UnaryOperation fw);
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.
vector<double> probabilities() const;
Returns: A vector<double> whose size member returns n and whose operator[] member returns when invoked with argument k for .