[rand.dist.samp.pconst]

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

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

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

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

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

[1](#1)

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

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](#eq:rand.dist.samp.pconst)[.](#1.sentence-1)

p(x|b0,â¦,bn,Ï0,â¦,Ïnâ1)=ÏiÂ , forÂ biâ¤x<bi+1(29.20)

[2](#2)

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

The n+1 distribution parameters bi,
also known as this distribution's [*interval boundaries*](#def:interval_boundaries), shall satisfy the relationbi<bi+1 for i=0,â¦,nâ1[.](#2.sentence-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*](#def:weights), shall be non-negative, non-NaN, and non-infinity[.](#2.sentence-2)

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

[ð](#lib:piecewise_constant_distribution_)

namespace std {template<class RealType = double>class 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<class UnaryOperation> piecewise_constant_distribution(initializer_list<RealType> bl, UnaryOperation fw); template<class UnaryOperation> 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<class URBG> result_type operator()(URBG& g); template<class URBG> 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); };}

[ð](#lib:piecewise_constant_distribution,constructor)

`piecewise_constant_distribution();
`

[3](#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[.](#3.sentence-1)

[ð](#lib:piecewise_constant_distribution,constructor_)

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

[4](#4)

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

*Mandates*: Both of

- [(4.1)](#4.1)

is_convertible_v<iterator_traits<InputIteratorB>​::​value_type, double>

- [(4.2)](#4.2)

is_convertible_v<iterator_traits<InputIteratorW>​::​value_type, double>

are true[.](#4.sentence-1)

[5](#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"))[.](#5.sentence-1)

If firstB == lastB or ++firstB == lastB,
 let n=1, w0=1, b0=0,
 and b1=1[.](#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[.](#5.sentence-3)

[6](#6)

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

*Effects*: Constructs a piecewise_constant_distribution object
 with parameters as specified above[.](#6.sentence-1)

[ð](#lib:piecewise_constant_distribution,constructor__)

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

[7](#7)

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

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

[8](#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[.](#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[.](#8.sentence-2)

[9](#9)

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

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

[ð](#lib:piecewise_constant_distribution,constructor___)

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

[10](#10)

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

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

[11](#11)

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

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

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

[12](#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[.](#12.sentence-1)

[13](#13)

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

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

[ð](#lib:intervals,piecewise_constant_distribution)

`vector<result_type> intervals() const;
`

[14](#14)

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

*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[.](#14.sentence-1)

[ð](#lib:densities,piecewise_constant_distribution)

`vector<result_type> densities() const;
`

[15](#15)

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

*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[.](#15.sentence-1)
