public member function
<random>

std::piecewise_linear_distribution::(ctor)

(1)
piecewise_linear_distribution();
(2)
template <class InputIteratorB, class InputIteratorW>  piecewise_linear_distribution (InputIteratorB firstB, InputIteratorB lastB,                                 InputIteratorW firstW);
(3)
template <class UnaryOperation>  piecewise_linear_distribution (initializer_list<result_type> il, UnaryOperation fw);
(4)
template <class UnaryOperation>  piecewise_linear_distribution (size_t nw, result_type xmin, result_type xmax, UnaryOperation fw);
(5)
 explicit piecewise_linear_distribution (const param_type& parm);
Construct piecewise linear distribution
Constructs a piecewise_linear_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 bound.
(3) initializer list constructor
The sequence in the list is used as the bounds for the subintervals. The weight given to each subinterval bound is the result of calling fw with the subinterval bound value as argument.
(4) operation constructor
The interval between xmin and xmax is split in nw subintervals of the same length. Each of the bounds of those subintervals is assigned as weight the result from calling:
1
fw(xmin+k*(xmax-xmin)/nw)
Where k is the order number of the subinterval bound (k=0 for the first subinterval bound, 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 for each interval bound. The number of elements used is the same as the number of subinterval bounds (i.e. distance(firstB,lastB)).
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
// piecewise_linear_distribution constructor
#include <iostream>
#include <chrono>
#include <random>
#include <array>
#include <cmath>

double square (double val) {return val*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,5> weights {10.0, 0.0, 5.0, 0.0, 10.0};

  typedef std::piecewise_linear_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, [](double x) {return std::sqrt(2*x);} );
  distribution_type fifth (fourth.param());

  std::cout << "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:
characteristics of fifth:
intervals: 0 2.5 5 7.5 10
densities: 0.059134 0.0836281 0.102423 0.118268 0.132228


Complexity

Linear in the number of subintervals, at most.

See also