Files
2025-10-25 03:02:53 +03:00

22 KiB
Raw Permalink Blame History

[rand.dist.samp]

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]

1

#

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

P(i|p0,…,pn−1)=pi(29.19)

2

#

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, shall be non-negative, non-NaN, and non-infinity.

Moreover, the following relation shall hold:0<S=w0+⋯+wn−1.

🔗

namespace std {templateclass 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 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); };}

🔗

discrete_distribution();

3

#

Effects: Constructs a discrete_distribution object with n=1 and p0=1.

[Note 1:

Such an object will always deliver the value 0.

— end note]

🔗

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

4

#

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

5

#

Preconditions: InputIterator meets the Cpp17InputIterator requirements ([input.iterators]).

If firstW == lastW, let n=1 and w0=1.

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

6

#

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

🔗

discrete_distribution(initializer_list<double> wl);

7

#

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

🔗

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

8

#

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

9

#

Preconditions: If nw=0, let n=1, otherwise let n=nw.

The relation 0<δ=(xmax−xmin)/n holds.

10

#

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

Otherwise, let wk=fw(xmin+kâ‹Î´+δ/2) for k=0,…,n−1.

11

#

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

🔗

vector<double> probabilities() const;

12

#

Returns: A vector whose size member returns n and whose operator[] member returns pk when invoked with argument k for k=0,…,n−1.

29.5.9.6.2 Class template piecewise_constant_distribution [rand.dist.samp.pconst]

1

#

A piecewise_constant_distribution random number distribution produces random numbers x,b0≤x<bn, uniformly distributed over each subinterval[bi,bi+1) according to the probability density function in Formula 29.20.

p(x|b0,…,bn,ρ0,…,ρˆ’1)=ρi , for bi≤x<bi+1(29.20)

2

#

The n+1 distribution parameters bi, also known as this distribution's interval boundaries, shall satisfy the relationbi<bi+1 for i=0,…,n−1.

Unless specified otherwise, the remaining n distribution parameters are calculated as:
ρk=wkSâ‹(bk+1−bk) for k=0,…,n−1 , in which the values wk, commonly known as the weights, shall be non-negative, non-NaN, and non-infinity.

Moreover, the following relation shall hold: 0<S=w0+⋯+wn−1.

🔗

namespace std {templateclass piecewise_constant_distribution {public:// typesusing result_type = RealType; using param_type = unspecified; // constructor and reset functions piecewise_constant_distribution(); template<class InputIteratorB, class InputIteratorW> 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<result_type> intervals() const; vector<result_type> densities() 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 piecewise_constant_distribution& x); template<class charT, class traits>friend basic_istream<charT, traits>&operator>>(basic_istream<charT, traits>& is, piecewise_constant_distribution& x); };}

🔗

piecewise_constant_distribution();

3

#

Effects: Constructs a piecewise_constant_distribution object with n=1, ρ0=1, b0=0, and b1=1.

🔗

template<class InputIteratorB, class InputIteratorW> piecewise_constant_distribution(InputIteratorB firstB, InputIteratorB lastB, InputIteratorW firstW);

4

#

Mandates: Both of

is_convertible_v<iterator_traits::value_type, double>

is_convertible_v<iterator_traits::value_type, double>

are true.

5

#

Preconditions: InputIteratorB and InputIteratorW each meet the Cpp17InputIterator requirements ([input.iterators]).

If firstB == lastB or ++firstB == lastB, let n=1, w0=1, b0=0, and b1=1.

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.

6

#

Effects: Constructs a piecewise_constant_distribution object with parameters as specified above.

🔗

template<class UnaryOperation> piecewise_constant_distribution(initializer_list<RealType> bl, UnaryOperation fw);

7

#

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

8

#

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.

Otherwise, let [bl.begin(), bl.end()) form a sequence b0,…,bn, and let wk=fw((bk+1+bk)/2) for k=0,…,n−1.

9

#

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

🔗

template<class UnaryOperation> piecewise_constant_distribution(size_t nw, RealType xmin, RealType xmax, UnaryOperation fw);

10

#

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

11

#

Preconditions: If nw=0, let n=1, otherwise let n=nw.

The relation 0<δ=(xmax−xmin)/n holds.

12

#

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.

13

#

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

🔗

vector<result_type> intervals() const;

14

#

Returns: A vector<result_type> whose size member returns n+1 and whose operator[] member returns bk when invoked with argument k for k=0,…,n.

🔗

vector<result_type> densities() const;

15

#

Returns: A vector<result_type> whose size member returns n and whose operator[] member returns ρk when invoked with argument k for k=0,…,n−1.

29.5.9.6.3 Class template piecewise_linear_distribution [rand.dist.samp.plinear]

1

#

A piecewise_linear_distribution random number distribution produces random numbers x,b0≤x<bn, distributed over each subinterval[bi,bi+1) according to the probability density function in Formula 29.21.

p(x|b0,…,bn,ρ0,…,ρn)=ρ‹bi+1−xbi+1−bi+ρi+1⋈’bibi+1−bi , for bi≤x<bi+1.(29.21)

2

#

The n+1 distribution parameters bi, also known as this distribution's interval boundaries, shall satisfy the relation bi<bi+1 for i=0,…,n−1.

Unless specified otherwise, the remaining n+1 distribution parameters are calculated asρk=wk/S for k=0,…,n, in which the values wk, commonly known as the weights at boundaries, shall be non-negative, non-NaN, and non-infinity.

Moreover, the following relation shall hold:
0<S=12⋈’ˆ‘k=0(wk+wk+1)â‹(bk+1−bk) .

🔗

namespace std {templateclass piecewise_linear_distribution {public:// typesusing result_type = RealType; using param_type = unspecified; // constructor and reset functions piecewise_linear_distribution(); template<class InputIteratorB, class InputIteratorW> 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<result_type> intervals() const; vector<result_type> densities() 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 piecewise_linear_distribution& x); template<class charT, class traits>friend basic_istream<charT, traits>&operator>>(basic_istream<charT, traits>& is, piecewise_linear_distribution& x); };}

🔗

piecewise_linear_distribution();

3

#

Effects: Constructs a piecewise_linear_distribution object with n=1, ρ0=ρ1=1, b0=0, and b1=1.

🔗

template<class InputIteratorB, class InputIteratorW> piecewise_linear_distribution(InputIteratorB firstB, InputIteratorB lastB, InputIteratorW firstW);

4

#

Mandates: Both of

is_convertible_v<iterator_traits::value_type, double>

is_convertible_v<iterator_traits::value_type, double>

are true.

5

#

Preconditions: InputIteratorB and InputIteratorW each meet the Cpp17InputIterator requirements ([input.iterators]).

If firstB == lastB or ++firstB == lastB, let n=1, ρ0=ρ1=1, b0=0, and b1=1.

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.

6

#

Effects: Constructs a piecewise_linear_distribution object with parameters as specified above.

🔗

template<class UnaryOperation> piecewise_linear_distribution(initializer_list<RealType> bl, UnaryOperation fw);

7

#

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

8

#

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.

Otherwise, let [bl.begin(), bl.end()) form a sequence b0,…,bn, and let wk=fw(bk) for k=0,…,n.

9

#

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

🔗

template<class UnaryOperation> piecewise_linear_distribution(size_t nw, RealType xmin, RealType xmax, UnaryOperation fw);

10

#

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

11

#

Preconditions: If nw=0, let n=1, otherwise let n=nw.

The relation 0<δ=(xmax−xmin)/n holds.

12

#

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.

13

#

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

🔗

vector<result_type> intervals() const;

14

#

Returns: A vector<result_type> whose size member returns n+1 and whose operator[] member returns bk when invoked with argument k for k=0,…,n.

🔗

vector<result_type> densities() const;

15

#

Returns: A vector<result_type> whose size member returns n and whose operator[] member returns ρk when invoked with argument k for k=0,…,n.