[rand.dist.samp] # 29 Numerics library [[numerics]](./#numerics) ## 29.5 Random number generation [[rand]](rand#dist.samp) ### 29.5.9 Random number distribution class templates [[rand.dist]](rand.dist#samp) #### 29.5.9.6 Sampling distributions [rand.dist.samp] #### [29.5.9.6.1](#discrete) Class template discrete_distribution [[rand.dist.samp.discrete]](rand.dist.samp.discrete) [1](#discrete-1) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/numerics.tex#L6273) A discrete_distribution random number distribution produces random integers i, 0≤iclass discrete_distribution {public:// typesusing result_type = IntType; using param_type = *unspecified*; // constructor and reset functions discrete_distribution(); template discrete_distribution(InputIterator firstW, InputIterator lastW); discrete_distribution(initializer_list wl); template 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 result_type operator()(URBG& g); template result_type operator()(URBG& g, const param_type& parm); // property functions vector probabilities() const; param_type param() const; void param(const param_type& parm); result_type min() const; result_type max() const; // inserters and extractorstemplatefriend basic_ostream&operator<<(basic_ostream& os, const discrete_distribution& x); templatefriend basic_istream&operator>>(basic_istream& is, discrete_distribution& x); };} [🔗](#lib:discrete_distribution,constructor) `discrete_distribution(); ` [3](#discrete-3) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/numerics.tex#L6346) *Effects*: Constructs a discrete_distribution object with n=1 and p0=1[.](#discrete-3.sentence-1) [*Note [1](#discrete-note-1)*: Such an object will always deliver the value 0[.](#discrete-3.sentence-2) — *end note*] [🔗](#lib:discrete_distribution,constructor_) `template discrete_distribution(InputIterator firstW, InputIterator lastW); ` [4](#discrete-4) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/numerics.tex#L6363) *Mandates*: is_convertible_v​::​value_type,double> is true[.](#discrete-4.sentence-1) [5](#discrete-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"))[.](#discrete-5.sentence-1) If firstW == lastW, let n=1 and w0=1[.](#discrete-5.sentence-2) Otherwise, [firstW, lastW) forms a sequence w of length n>0[.](#discrete-5.sentence-3) [6](#discrete-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)[.](#discrete-6.sentence-1) [🔗](#lib:discrete_distribution,constructor__) `discrete_distribution(initializer_list wl); ` [7](#discrete-7) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/numerics.tex#L6391) *Effects*: Same as discrete_distribution(wl.begin(), wl.end())[.](#discrete-7.sentence-1) [🔗](#lib:discrete_distribution,constructor___) `template discrete_distribution(size_t nw, double xmin, double xmax, UnaryOperation fw); ` [8](#discrete-8) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/numerics.tex#L6403) *Mandates*: is_invocable_r_v is true[.](#discrete-8.sentence-1) [9](#discrete-9) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/numerics.tex#L6407) *Preconditions*: If nw=0, let n=1, otherwise let n=nw[.](#discrete-9.sentence-1) The relation 0<δ=(xmax−xmin)/n holds[.](#discrete-9.sentence-2) [10](#discrete-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[.](#discrete-10.sentence-1) Otherwise, let wk=fw(xmin+kâ‹Î´+δ/2) for k=0,…,n−1[.](#discrete-10.sentence-2) [11](#discrete-11) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/numerics.tex#L6425) *Complexity*: The number of invocations of fw does not exceed n[.](#discrete-11.sentence-1) [🔗](#lib:probabilities,discrete_distribution) `vector probabilities() const; ` [12](#discrete-12) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/numerics.tex#L6436) *Returns*: A vector whose size member returns n and whose operator[] member returns pk when invoked with argument k for k=0,…,n−1[.](#discrete-12.sentence-1) #### [29.5.9.6.2](#pconst) Class template piecewise_constant_distribution [[rand.dist.samp.pconst]](rand.dist.samp.pconst) [1](#pconst-1) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/numerics.tex#L6452) A piecewise_constant_distribution random number distribution produces random numbers x,b0≤xclass piecewise_constant_distribution {public:// typesusing result_type = RealType; using param_type = *unspecified*; // constructor and reset functions piecewise_constant_distribution(); template piecewise_constant_distribution(InputIteratorB firstB, InputIteratorB lastB, InputIteratorW firstW); template piecewise_constant_distribution(initializer_list bl, UnaryOperation fw); template piecewise_constant_distribution(size_t nw, RealType xmin, RealType xmax, UnaryOperation fw); explicit piecewise_constant_distribution(const param_type& parm); void reset(); // equality operatorsfriend bool operator==(const piecewise_constant_distribution& x, const piecewise_constant_distribution& y); // generating functionstemplate result_type operator()(URBG& g); template result_type operator()(URBG& g, const param_type& parm); // property functions vector intervals() const; vector densities() const; param_type param() const; void param(const param_type& parm); result_type min() const; result_type max() const; // inserters and extractorstemplatefriend basic_ostream&operator<<(basic_ostream& os, const piecewise_constant_distribution& x); templatefriend basic_istream&operator>>(basic_istream& is, piecewise_constant_distribution& x); };} [🔗](#lib:piecewise_constant_distribution,constructor) `piecewise_constant_distribution(); ` [3](#pconst-3) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/numerics.tex#L6538) *Effects*: Constructs a piecewise_constant_distribution object with n=1, ρ0=1, b0=0, and b1=1[.](#pconst-3.sentence-1) [🔗](#lib:piecewise_constant_distribution,constructor_) `template piecewise_constant_distribution(InputIteratorB firstB, InputIteratorB lastB, InputIteratorW firstW); ` [4](#pconst-4) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/numerics.tex#L6556) *Mandates*: Both of - [(4.1)](#pconst-4.1) is_convertible_v​::​value_type, double> - [(4.2)](#pconst-4.2) is_convertible_v​::​value_type, double> are true[.](#pconst-4.sentence-1) [5](#pconst-5) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/numerics.tex#L6565) *Preconditions*: InputIteratorB and InputIteratorW each meet the [*Cpp17InputIterator*](input.iterators#:Cpp17InputIterator "24.3.5.3 Input iterators [input.iterators]") requirements ([[input.iterators]](input.iterators "24.3.5.3 Input iterators"))[.](#pconst-5.sentence-1) If firstB == lastB or ++firstB == lastB, let n=1, w0=1, b0=0, and b1=1[.](#pconst-5.sentence-2) Otherwise, [firstB, lastB) forms a sequence b of length n+1, the length of the sequence w starting from firstW is at least n, and any wk for k ≥ n are ignored by the distribution[.](#pconst-5.sentence-3) [6](#pconst-6) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/numerics.tex#L6583) *Effects*: Constructs a piecewise_constant_distribution object with parameters as specified above[.](#pconst-6.sentence-1) [🔗](#lib:piecewise_constant_distribution,constructor__) `template piecewise_constant_distribution(initializer_list bl, UnaryOperation fw); ` [7](#pconst-7) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/numerics.tex#L6597) *Mandates*: is_invocable_r_v is true[.](#pconst-7.sentence-1) [8](#pconst-8) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/numerics.tex#L6601) *Effects*: Constructs a piecewise_constant_distribution object with parameters taken or calculated from the following values: If bl.size()<2, let n=1, w0=1, b0=0, and b1=1[.](#pconst-8.sentence-1) Otherwise, let [bl.begin(), bl.end()) form a sequence b0,…,bn, and let wk=fw((bk+1+bk)/2) for k=0,…,n−1[.](#pconst-8.sentence-2) [9](#pconst-9) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/numerics.tex#L6618) *Complexity*: The number of invocations of fw does not exceed n[.](#pconst-9.sentence-1) [🔗](#lib:piecewise_constant_distribution,constructor___) `template piecewise_constant_distribution(size_t nw, RealType xmin, RealType xmax, UnaryOperation fw); ` [10](#pconst-10) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/numerics.tex#L6631) *Mandates*: is_invocable_r_v is true[.](#pconst-10.sentence-1) [11](#pconst-11) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/numerics.tex#L6635) *Preconditions*: If nw=0, let n=1, otherwise let n=nw[.](#pconst-11.sentence-1) The relation 0<δ=(xmax−xmin)/n holds[.](#pconst-11.sentence-2) [12](#pconst-12) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/numerics.tex#L6641) *Effects*: Constructs a piecewise_constant_distribution object with parameters taken or calculated from the following values: Let bk=xmin+kâ‹Î´ for k=0,…,n, and wk=fw(bk+δ/2) for k=0,…,n−1[.](#pconst-12.sentence-1) [13](#pconst-13) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/numerics.tex#L6649) *Complexity*: The number of invocations of fw does not exceed n[.](#pconst-13.sentence-1) [🔗](#lib:intervals,piecewise_constant_distribution) `vector intervals() const; ` [14](#pconst-14) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/numerics.tex#L6660) *Returns*: A vector whose size member returns n+1 and whose operator[] member returns bk when invoked with argument k for k=0,…,n[.](#pconst-14.sentence-1) [🔗](#lib:densities,piecewise_constant_distribution) `vector densities() const; ` [15](#pconst-15) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/numerics.tex#L6674) *Returns*: A vector whose size member returns n and whose operator[] member returns ρk when invoked with argument k for k=0,…,n−1[.](#pconst-15.sentence-1) #### [29.5.9.6.3](#plinear) Class template piecewise_linear_distribution [[rand.dist.samp.plinear]](rand.dist.samp.plinear) [1](#plinear-1) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/numerics.tex#L6690) A piecewise_linear_distribution random number distribution produces random numbers x,b0≤xclass piecewise_linear_distribution {public:// typesusing result_type = RealType; using param_type = *unspecified*; // constructor and reset functions piecewise_linear_distribution(); template piecewise_linear_distribution(InputIteratorB firstB, InputIteratorB lastB, InputIteratorW firstW); template piecewise_linear_distribution(initializer_list bl, UnaryOperation fw); template piecewise_linear_distribution(size_t nw, RealType xmin, RealType xmax, UnaryOperation fw); explicit piecewise_linear_distribution(const param_type& parm); void reset(); // equality operatorsfriend bool operator==(const piecewise_linear_distribution& x, const piecewise_linear_distribution& y); // generating functionstemplate result_type operator()(URBG& g); template result_type operator()(URBG& g, const param_type& parm); // property functions vector intervals() const; vector densities() const; param_type param() const; void param(const param_type& parm); result_type min() const; result_type max() const; // inserters and extractorstemplatefriend basic_ostream&operator<<(basic_ostream& os, const piecewise_linear_distribution& x); templatefriend basic_istream&operator>>(basic_istream& is, piecewise_linear_distribution& x); };} [🔗](#lib:piecewise_linear_distribution,constructor) `piecewise_linear_distribution(); ` [3](#plinear-3) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/numerics.tex#L6774) *Effects*: Constructs a piecewise_linear_distribution object with n=1, ρ0=ρ1=1, b0=0, and b1=1[.](#plinear-3.sentence-1) [🔗](#lib:piecewise_linear_distribution,constructor_) `template piecewise_linear_distribution(InputIteratorB firstB, InputIteratorB lastB, InputIteratorW firstW); ` [4](#plinear-4) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/numerics.tex#L6791) *Mandates*: Both of - [(4.1)](#plinear-4.1) is_convertible_v​::​value_type, double> - [(4.2)](#plinear-4.2) is_convertible_v​::​value_type, double> are true[.](#plinear-4.sentence-1) [5](#plinear-5) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/numerics.tex#L6800) *Preconditions*: InputIteratorB and InputIteratorW each meet the [*Cpp17InputIterator*](input.iterators#:Cpp17InputIterator "24.3.5.3 Input iterators [input.iterators]") requirements ([[input.iterators]](input.iterators "24.3.5.3 Input iterators"))[.](#plinear-5.sentence-1) If firstB == lastB or ++firstB == lastB, let n=1, ρ0=ρ1=1, b0=0, and b1=1[.](#plinear-5.sentence-2) Otherwise, [firstB, lastB) forms a sequence b of length n+1, the length of the sequence w starting from firstW is at least n+1, and any wk for k≥n+1 are ignored by the distribution[.](#plinear-5.sentence-3) [6](#plinear-6) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/numerics.tex#L6818) *Effects*: Constructs a piecewise_linear_distribution object with parameters as specified above[.](#plinear-6.sentence-1) [🔗](#lib:piecewise_linear_distribution,constructor__) `template piecewise_linear_distribution(initializer_list bl, UnaryOperation fw); ` [7](#plinear-7) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/numerics.tex#L6832) *Mandates*: is_invocable_r_v is true[.](#plinear-7.sentence-1) [8](#plinear-8) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/numerics.tex#L6836) *Effects*: Constructs a piecewise_linear_distribution object with parameters taken or calculated from the following values: If bl.size()<2, let n=1, ρ0=ρ1=1, b0=0, and b1=1[.](#plinear-8.sentence-1) Otherwise, let [bl.begin(), bl.end()) form a sequence b0,…,bn, and let wk=fw(bk) for k=0,…,n[.](#plinear-8.sentence-2) [9](#plinear-9) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/numerics.tex#L6853) *Complexity*: The number of invocations of fw does not exceed n+1[.](#plinear-9.sentence-1) [🔗](#lib:piecewise_linear_distribution,constructor___) `template piecewise_linear_distribution(size_t nw, RealType xmin, RealType xmax, UnaryOperation fw); ` [10](#plinear-10) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/numerics.tex#L6866) *Mandates*: is_invocable_r_v is true[.](#plinear-10.sentence-1) [11](#plinear-11) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/numerics.tex#L6870) *Preconditions*: If nw=0, let n=1, otherwise let n=nw[.](#plinear-11.sentence-1) The relation 0<δ=(xmax−xmin)/n holds[.](#plinear-11.sentence-2) [12](#plinear-12) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/numerics.tex#L6876) *Effects*: Constructs a piecewise_linear_distribution object with parameters taken or calculated from the following values: Let bk=xmin+kâ‹Î´ for k=0,…,n, and wk=fw(bk) for k=0,…,n[.](#plinear-12.sentence-1) [13](#plinear-13) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/numerics.tex#L6884) *Complexity*: The number of invocations of fw does not exceed n+1[.](#plinear-13.sentence-1) [🔗](#lib:intervals,piecewise_linear_distribution) `vector intervals() const; ` [14](#plinear-14) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/numerics.tex#L6895) *Returns*: A vector whose size member returns n+1 and whose operator[] member returns bk when invoked with argument k for k=0,…,n[.](#plinear-14.sentence-1) [🔗](#lib:densities,piecewise_linear_distribution) `vector densities() const; ` [15](#plinear-15) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/numerics.tex#L6909) *Returns*: A vector whose size member returns n and whose operator[] member returns ρk when invoked with argument k for k=0,…,n[.](#plinear-15.sentence-1)