公有成員函式
<random>

std::piecewise_constant_distribution::(建構函式)

(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);
構造分段常數分佈
構造一個 piecewise_constant_distribution 物件,根據所使用的建構函式版本進行初始化。

(1) 預設建構函式
該分佈生成範圍內均勻分佈的隨機數。[0.0,1.0).
(2) 範圍建構函式 ((2) range constructor)
範圍內的值[firstB,lastB)用作子區間的*界限*(bi),而以...開頭的序列firstW用作每個子區間的*權重*(wi)。
(3) 初始化列表建構函式
列表中的序列用作子區間的*界限*。每個子區間的權重是透過呼叫fw並以子區間中點(即其界限值之和的一半)作為引數獲得的。
(4) 操作建構函式 ((4) operation constructor)
範圍 [xmin, xmax) 被劃分為 nw 個等長的子區間。每個子區間將獲得呼叫
1
fw(xmin+(xmax-xmin)*(k+0.5)/nw)
其中 k 是子區間的序號(k=0對於第一個子區間,k=1對於第二個子區間,...)。
(5) 引數建構函式 ((5) param constructor)
該分佈採用 parm 指定的分佈引數。 (The distribution adopts the distribution parameters specified by parm.)

在以上所有情況下,如果界限數量少於 2(因此不足以形成一個區間),則分佈產生的數值與預設構造時相同(生成範圍內均勻分佈的數值。[0,1)).

所有權重必須是非負值,並且序列中的至少一個值必須是正值。 (All weights shall be non-negative values, and at least one of the values in the sequence must be positive.)

引數

firstB, lastB
輸入迭代器,指向指定子區間界限值範圍的起始和結束位置。
使用的範圍是[firstB,lastB),包括 first 指向的元素以及 firstBlastB 之間的所有元素,但不包括 lastB 指向的元素。
如果已知firstB==lastB型別,或++firstB==lastB,該分佈將生成範圍內均勻分佈的數值。[0,1).
函式模板型別應為 輸入迭代器,指向可轉換為 (The function template type shall be an input iterator that points to some type convertible to)double.
firstW
輸入迭代器,指向指定用於每個區間的權重的起始位置。使用的元素數量比子區間界限的數量少一個(即distance(firstB,lastB)-1).
il
一個 initializer_list 物件,包含一個子區間界限列表。
如果*初始化列表*少於2個元素,則分佈將生成範圍內均勻分佈的數值。[0,1).
這些物件是從初始化列表宣告符自動構造的。
fw
可以接受單個引數的類函式物件,該引數可從...轉換為double並返回一個可轉換為以下型別的值 (and return a value convertible to)。這可以是指向函式的指標、函式物件或 lambda 表示式, (This can can be a pointer to a function, a function object, or a lambda expression,)double。這可以是函式指標、函式物件或 lambda 表示式,
nw
由...定義的區間中子區間的數量[xmin,xmax)被分割。
如果設定為零,則假定有一個子區間。
size_t 是一個無符號整數型別。
xmin,xmax
分佈可能值的區間界限。此區間被分割成nw等長的子區間。
xmax必須大於xmin (xmin<xmax).
result_type是一個成員型別,定義為第一個類模板引數的別名 (實數型別 (RealType)).
parm
一個表示分佈引數的物件,透過呼叫成員函式 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
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;
}

可能的輸出
displaying characteristics of fifth:
intervals: 0 2.5 5 7.5 10
densities: 0.0133333 0.0533333 0.12 0.213333


複雜度

線性複雜度,子區間數量最多。

另見