[rand.dist.samp.discrete]

# 29 Numerics library [[numerics]](./#numerics)

## 29.5 Random number generation [[rand]](rand#dist.samp.discrete)

### 29.5.9 Random number distribution class templates [[rand.dist]](rand.dist#samp.discrete)

#### 29.5.9.6 Sampling distributions [[rand.dist.samp]](rand.dist.samp#discrete)

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

[1](#1)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/numerics.tex#L6273)

A discrete_distribution random number distribution
produces random integers i, 0â¤i<n,
distributed according to
the discrete probability function in Formula [29.19](#eq:rand.dist.samp.discrete)[.](#1.sentence-1)

P(i|p0,â¦,pnâ1)=pi(29.19)

[2](#2)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/numerics.tex#L6282)

Unless specified otherwise,
the distribution parameters are calculated as:pk=wk/S for k=0,â¦,nâ1,
in which the values wk,
commonly known as the [*weights*](#def:weights), shall be non-negative, non-NaN, and non-infinity[.](#2.sentence-1)

Moreover, the following relation shall hold:0<S=w0+â¯+wnâ1[.](#2.sentence-2)

[ð](#lib:discrete_distribution_)

namespace std {template<class IntType = int>class discrete_distribution {public:// typesusing 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 operatorsfriend bool operator==(const discrete_distribution& x, const discrete_distribution& y); // generating functionstemplate<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 extractorstemplate<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); };}

[ð](#lib:discrete_distribution,constructor)

`discrete_distribution();
`

[3](#3)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/numerics.tex#L6346)

*Effects*: Constructs a discrete_distribution object
with n=1 and p0=1[.](#3.sentence-1)

[*Note [1](#note-1)*:

Such an object will always deliver the value 0[.](#3.sentence-2)

â *end note*]

[ð](#lib:discrete_distribution,constructor_)

`template<class InputIterator>
  discrete_distribution(InputIterator firstW, InputIterator lastW);
`

[4](#4)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/numerics.tex#L6363)

*Mandates*: is_convertible_v<iterator_traits<InputIterator>​::​value_type,double> is true[.](#4.sentence-1)

[5](#5)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/numerics.tex#L6368)

*Preconditions*: InputIterator meets the [*Cpp17InputIterator*](input.iterators#:Cpp17InputIterator "24.3.5.3 Input iterators [input.iterators]") requirements ([[input.iterators]](input.iterators "24.3.5.3 Input iterators"))[.](#5.sentence-1)

If firstW == lastW,
 let n=1 and w0=1[.](#5.sentence-2)

Otherwise, [firstW, lastW) forms a sequence w of length n>0[.](#5.sentence-3)

[6](#6)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/numerics.tex#L6378)

*Effects*: Constructs a discrete_distribution object
with probabilities given by the Formula [29.19](#eq:rand.dist.samp.discrete)[.](#6.sentence-1)

[ð](#lib:discrete_distribution,constructor__)

`discrete_distribution(initializer_list<double> wl);
`

[7](#7)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/numerics.tex#L6391)

*Effects*: Same as discrete_distribution(wl.begin(), wl.end())[.](#7.sentence-1)

[ð](#lib:discrete_distribution,constructor___)

`template<class UnaryOperation>
  discrete_distribution(size_t nw, double xmin, double xmax, UnaryOperation fw);
`

[8](#8)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/numerics.tex#L6403)

*Mandates*: is_invocable_r_v<double, UnaryOperation&, double> is true[.](#8.sentence-1)

[9](#9)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/numerics.tex#L6407)

*Preconditions*: If nw=0, let n=1, otherwise let n=nw[.](#9.sentence-1)

The relation 0<Î´=(xmaxâxmin)/n holds[.](#9.sentence-2)

[10](#10)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/numerics.tex#L6414)

*Effects*: Constructs a discrete_distribution object
 with probabilities given by the formula above,
 using the following values:
 If nw=0,
 let w0=1[.](#10.sentence-1)

Otherwise,
 let wk=fw(xmin+kâÎ´+Î´/2) for k=0,â¦,nâ1[.](#10.sentence-2)

[11](#11)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/numerics.tex#L6425)

*Complexity*: The number of invocations of fw does not exceed n[.](#11.sentence-1)

[ð](#lib:probabilities,discrete_distribution)

`vector<double> probabilities() const;
`

[12](#12)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/numerics.tex#L6436)

*Returns*: A vector<double> whose size member returns n and whose operator[] member returns pk when invoked with argument k for k=0,â¦,nâ1[.](#12.sentence-1)
