<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.)

另見