29 Numerics library [numerics]

29.6 Random number generation [rand]

29.6.8 Random number distribution class templates [rand.dist]

29.6.8.6 Sampling distributions [rand.dist.samp]

29.6.8.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

Unless specified otherwise, the distribution parameters are calculated as: , in which the values , commonly known as the weights, shall be non-negative, non-NaN, and non-infinity.
Moreover, the following relation shall hold: .
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();

    // 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;
  };
discrete_distribution();
Effects: Constructs a discrete_­distribution object with and .
[Note
:
Such an object will always deliver the value 0.
end note
]
template<class InputIterator> discrete_distribution(InputIterator firstW, InputIterator lastW);
Requires: InputIterator shall satisfy the requirements of an input iterator ([input.iterators]).
Moreover, iterator_­traits<InputIterator>​::​value_­type shall denote a type that is convertible to double.
If firstW == lastW, let and .
Otherwise, shall form a sequence w of length .
Effects: Constructs a discrete_­distribution object with probabilities given by the formula above.
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);
Requires: Each instance of type UnaryOperation shall be a function object ([function.objects]) whose return type shall be convertible to double.
Moreover, double shall be convertible to the type of UnaryOperation's sole parameter.
If , let , otherwise let .
The relation shall hold.
Effects: Constructs a discrete_­distribution object with probabilities given by the formula above, using the following values: If nw, let .
Otherwise, let for .
Complexity: The number of invocations of fw shall 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 .