公有成員函式 (public member function)
<random>
(1) | discrete_distribution(); |
---|
(2) | template <class InputIterator> discrete_distribution (InputIterator first, InputIterator last); |
---|
(3) | discrete_distribution (initializer_list<double> il); |
---|
(4) | template <class UnaryOperation> discrete_distribution (size_t nw, double xmin, double xmax, UnaryOperation fw); |
---|
(5) | explicit discrete_distribution (const param_type& parm); |
---|
構造離散分佈 (Construct discrete distribution)
構造一個 discrete_distribution 物件,其初始化方式取決於所使用的建構函式版本
- (1) 預設建構函式
- 該分佈將始終產生零。
- (2) 範圍建構函式 ((2) range constructor)
- 該序列的值被用作範圍中每個整數值的權重,從0為(n-1),其中n是迭代器之間的距離。
- (3) 初始化列表 (initializer list)
- 該列表中的序列被用作每個整數值的權重,從0為(n-1),其中n是 初始化列表的大小。
- (4) 操作建構函式 ((4) operation constructor)
- 閉區間 [min, max] 中的每個整數值 k0和(nw-1)被賦予透過呼叫得到的權重
1
|
fw(xmin+(xmax-xmin)*(k+0.5)/nw)
|
- (5) 引數建構函式 ((5) param constructor)
- 該分佈採用 parm 指定的分佈引數。 (The distribution adopts the distribution parameters specified by parm.)
在所有上述情況中,如果n(或nw) 為零,則分佈構造為好像定義了一個帶有權重為一(w0=1)的單個元素。這樣的分佈將始終產生零。
所有權重必須是非負值,並且序列中的至少一個值必須是正值。 (All weights shall be non-negative values, and at least one of the values in the sequence must be positive.)
引數
- first, last
- 指向範圍內初始位置和最終位置的輸入迭代器。 使用的範圍是[first,last),它包括first和last之間的所有元素,包括first指向的元素,但不包括last指向的元素。
如果已知first==last,分佈將始終產生零。
函式模板型別應為 輸入迭代器,指向可轉換為 (The function template type shall be an input iterator that points to some type convertible to)double.
- il
- il
這些物件是從初始化列表宣告符自動構造的。
- nw
- 權重數量:分佈能產生的最大數量為(nw-1).
- xmin,xmax
- 傳遞給 fw 的值範圍的界限。傳遞給 fw 的第一個值是(xmin+(xmax-xmin)/2*n)然後,nw 個值以遞增量((xmax-xmin)/2).
- 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.
- 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
|
// discrete_distribution constructor
#include <iostream>
#include <chrono>
#include <random>
#include <functional>
#include <array>
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,4> init = {1.0,2.0,3.0,4.0};
std::discrete_distribution<int> first;
std::discrete_distribution<int> second (init.begin(),init.end());
std::discrete_distribution<int> third {0.1,0.2,0.3,0.4};
std::discrete_distribution<int> fourth (4,0.0,40.0,std::bind2nd(std::plus<double>(),5.0));
std::discrete_distribution<int> fifth (fourth.param());
// display probabilities:
std::cout << "displaying probabilities:";
std::cout << std::endl << "first : ";
for (double x:first.probabilities()) std::cout << x << " ";
std::cout << std::endl << "second: ";
for (double x:second.probabilities()) std::cout << x << " ";
std::cout << std::endl << "third : ";
for (double x:third.probabilities()) std::cout << x << " ";
std::cout << std::endl << "fourth: ";
for (double x:fourth.probabilities()) std::cout << x << " ";
std::cout << std::endl << "fifth : ";
for (double x:fifth.probabilities()) std::cout << x << " ";
std::cout << std::endl;
return 0;
}
|
輸出
displaying probabilities:
first : 1
second: 0.1 0.2 0.3 0.4
third : 0.1 0.2 0.3 0.4
fourth: 0.1 0.2 0.3 0.4
fifth : 0.1 0.2 0.3 0.4
|