<random>

公有成员函数 (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)
构造一个 piecewise_linear_distribution 对象,根据使用的构造函数版本进行初始化 (Constructs a piecewise_linear_distribution object, initializing it depending on the constructor version used)

(1) 默认构造函数
该分布生成在范围内的随机数 (The distribution produces random numbers uniformly distributed in the range)[0.0,1.0).
(2) 范围构造函数 ((2) range constructor)
范围内的值 (The values in the range)[firstB,lastB)用作子区间的*边界* (bi),而以 firstW 开始的序列用作每个子区间边界的*权重* (wi) (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.)firstW是每个子区间边界的*权重* (wi) (is used as the weights (wi) for each subinterval bound.)
(3) 列表初始化构造函数 ((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) 操作构造函数 ((4) operation constructor)
xminxmax 之间的区间被划分为 *nw* 个等长子区间。这些子区间的每个边界都被指定权重,权重值是通过调用以下函数得到的 (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)
其中 k 是子区间边界的序号 ( (1) Where k is the order number of the subinterval bound (k=0第一个子区间边界,k=1第二个,...)。
(5) 参数构造函数 ((5) param constructor)
该分布采用 parm 指定的分布参数。 (The distribution adopts the distribution parameters specified by parm.)

在以上所有情况下,如果边界数量少于 2(因此不足以形成一个区间),则该分布产生的数值与默认构造时相同(生成在范围内的均匀分布数值 (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.)

参数

firstB, lastB
输入迭代器,指向用于指定子区间边界值范围的初始和末尾位置。 (Input iterators to the initial and final positions in a range of values specifying the subinterval bounds.)
使用的范围是[firstB,lastB)包括 firstB 指向的元素,但不包括 lastB 指向的元素。 (which includes all the elements between firstB and lastB, including the element pointed by first but not the element pointed by lastB.)
如果已知firstB==lastB类型,或++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).
这些对象是从初始化列表声明符自动构造的。
fw
一个类似函数的对象,它可以接受一个可从以下类型转换的单个参数 (A function-like object that can take a single argument of a type convertible from)double并返回一个可转换为以下类型的值 (and return a value convertible to)。这可以是指向函数的指针、函数对象或 lambda 表达式, (This can can be a pointer to a function, a function object, or a lambda expression,)doublelambda 表达式 (lambda expression)
nw
将 [xmin,xmax) 定义的区间划分为的子区间数量。 (Number of subintervals in which the interval defined by [xmin,xmax) is split.)[xmin,xmax)为等长子区间。 (subintervals of the same length.)
如果设置为零,则假定为一个子区间。 (If set to zero, one subinterval is assumed.)
size_t 是一个无符号整数类型。
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是一个成员类型,定义为第一个类模板参数的别名 (实数类型 (RealType)).
parm
一个表示分布参数的对象,通过调用成员函数 param 获得。 (An object representing the distribution's parameters, obtained by a call to member function param.)
param_type是一个成员类型。

示例

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;
}

可能的输出
characteristics of fifth:
intervals: 0 2.5 5 7.5 10
densities: 0.059134 0.0836281 0.102423 0.118268 0.132228


复杂度

线性复杂度,最多为子区间数量。 (Linear in the number of subintervals, at most.)

另见