public member function
<random>

std::piecewise_constant_distribution::(ctor)

(1)
piecewise_constant_distribution();
(2)
template <class InputIteratorB, class InputIteratorW>  piecewise_constant_distribution (InputIteratorB firstB, InputIteratorB lastB,                                   InputIteratorW firstW);
(3)
template <class UnaryOperation>  piecewise_constant_distribution (initializer_list<result_type> il, UnaryOperation fw);
(4)
template <class UnaryOperation>  piecewise_constant_distribution (size_t nw, result_type xmin, result_type xmax, UnaryOperation fw);
(5)
 explicit piecewise_constant_distribution (const param_type& parm);
Construct piecewise constant distribution
Constructs a piecewise_constant_distribution object, initializing it depending on the constructor version used:

(1) default constructor
The distribution produces random numbers uniformly distributed in the range [0.0,1.0).
(2) range constructor
The values in the range [firstB,lastB) are used as the bounds for the subintervals (bi) and the sequence beginning at firstW is used as the weights (wi) for each subinterval.
(3) initializer list constructor
The sequence in the list is used as the bounds for the subintervals. The weight given to each subinterval is the result of calling fw with the midpoint of the subinterval (i.e., half the sum of its bound values) as argument.
(4) operation constructor
The interval between xmin and xmax is split in nw subintervals of the same length. Each resulting subinterval is assigned the result from calling:
1
fw(xmin+(xmax-xmin)*(k+0.5)/nw)
Where k is the order number of the subinterval (k=0 for the first subinterval, k=1 for the second, ...).
(5) param constructor
The distribution adopts the distribution parameters specified by parm.

In all the cases above, if the number of bounds is lower than 2 (and thus insufficient to form an interval), the distribution produces the same values as if default-constructed (produces numbers uniformly distributed in the range [0,1)).

All weights shall be non-negative values, and at least one of the values in the sequence must be positive.

Parameters

firstB, lastB
Input iterators to the initial and final positions in a range of values specifying the subinterval bounds.
The range used is [firstB,lastB), which includes all the elements between firstB and lastB, including the element pointed by first but not the element pointed by lastB.
If firstB==lastB, or ++firstB==lastB, the distribution will produce values uniformly distributed in the range [0,1).
The function template type shall be an input iterator that points to some type convertible to double.
firstW
Input iterator to the initial position in a range of values specifying the weights to be used on each interval. The number of elements used is one less than the number of subinterval bounds (i.e. distance(firstB,lastB)-1).
il
An initializer_list object, with a list of subinterval bounds.
If the initializer list has less than 2 elements, the distribution will produce values uniformly distributed in the range [0,1).
These objects are automatically constructed from initializer list declarators.
fw
A function-like object that can take a single argument of a type convertible from double and return a value convertible to double. This can can be a pointer to a function, a function object, or a lambda expression,
nw
Number of subintervals in which the interval defined by [xmin,xmax) is split.
If set to zero, one subinterval is assumed.
size_t is an unsigned integral type.
xmin,xmax
Bounds of the interval of possible values for the distribution. This interval is split into nw subintervals of the same length.
xmax shall be greater than xmin (xmin<xmax).
result_type is a member type, defined as an alias of the first class template parameter (RealType).
parm
An object representing the distribution's parameters, obtained by a call to member function param.
param_type is a member type.

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
// piecewise_constant_distribution constructor
#include <iostream>
#include <chrono>
#include <random>
#include <array>

double square (double val) {return val*val;}

class accumulator {
  double x;
public:
  accumulator() :x(0.0) {}
  double operator()(double val) {return x+=val;}
};

int main()
{
  // construct a trivial random generator engine from a time-based seed:
  unsigned seed = std::chrono::system_clock::now().time_since_epoch().count();
  std::default_random_engine generator (seed);

  std::array<double,5> intervals {0.0, 10.0, 20.0, 30.0, 40.0};
  std::array<double,4> weights {1.0, 2.0, 3.0, 4.0};

  typedef std::piecewise_constant_distribution<double> distribution_type;
  distribution_type first;
  distribution_type second (intervals.begin(), intervals.end(), weights.begin());
  distribution_type third ( {0.0, 10.0, 15.0, 20.0, 22.0}, square );
  distribution_type fourth ( 4, 0.0, 10.0, accumulator() );
  distribution_type fifth (fourth.param());

  std::cout << "displaying characteristics of fifth:" << std::endl;
  std::cout << "intervals: ";
  for (double x:fifth.intervals()) std::cout << x << " ";
  std::cout << std::endl;
  std::cout << "densities: ";
  for (double x:fifth.densities()) std::cout << x << " ";
  std::cout << std::endl;

  return 0;
}

Possible output:
displaying characteristics of fifth:
intervals: 0 2.5 5 7.5 10
densities: 0.0133333 0.0533333 0.12 0.213333


Complexity

Linear in the number of subintervals, at most.

See also