# 28 Numerics library [numerics]

## 28.5 Random number generation [rand]

### 28.5.9 Random number distribution class templates [rand.dist]

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

A piecewise_constant_distribution random number distribution produces random numbers x, , uniformly distributed over each subinterval according to the probability density function in Formula 28.20.
The distribution parameters , also known as this distribution's interval boundaries, shall satisfy the relation for .
Unless specified otherwise, the remaining n 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: .
namespace std { template<class RealType = double> class piecewise_constant_distribution { public: // types using 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 operators friend bool operator==(const piecewise_constant_distribution& x, const piecewise_constant_distribution& y); // 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<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 extractors template<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(); ```
Effects: Constructs a piecewise_constant_distribution object with , , , and .
```template<class InputIteratorB, class InputIteratorW> piecewise_constant_distribution(InputIteratorB firstB, InputIteratorB lastB, InputIteratorW firstW); ```
Mandates: Both of
• is_convertible_v<iterator_traits<InputIteratorB>​::​value_type, double>
• is_convertible_v<iterator_traits<InputIteratorW>​::​value_type, double>
are true.
Preconditions: InputIteratorB and InputIteratorW each meet the Cpp17InputIterator requirements ([input.iterators]).
If firstB == lastB or ++firstB == lastB, let , , , and .
Otherwise, [firstB, lastB) forms a sequence b of length , the length of the sequence w starting from firstW is at least n, and any for k  ≥ n are ignored by the distribution.
Effects: Constructs a piecewise_constant_distribution object with parameters as specified above.
```template<class UnaryOperation> piecewise_constant_distribution(initializer_list<RealType> bl, UnaryOperation fw); ```
Mandates: is_invocable_r_v<double, UnaryOperation&, double> is true.
Effects: Constructs a piecewise_constant_distribution object with parameters taken or calculated from the following values: If , let , , , and .
Otherwise, let [bl.begin(), bl.end()) form a sequence , and let for .
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); ```
Mandates: is_invocable_r_v<double, UnaryOperation&, double> is true.
Preconditions: If , let , otherwise let .
The relation holds.
Effects: Constructs a piecewise_constant_distribution object with parameters taken or calculated from the following values: Let for , and for .
Complexity: The number of invocations of fw does not exceed n.
```vector<result_type> intervals() const; ```
Returns: A vector<result_type> whose size member returns and whose operator[] member returns when invoked with argument k for .
```vector<result_type> densities() const; ```
Returns: A vector<result_type> whose size member returns n and whose operator[] member returns when invoked with argument k for .