目錄
資料參考《C++標準庫》第二版
1、隨機數
1)引擎:隨機性的源頭,用來產生隨機數;
2)分佈:隨機值,隨機數的隨機範圍;
3)隨機數:利用引擎在分佈範圍內隨機選一個值。
2、引擎
2.1 引擎的種類
基礎引擎
linear_congruential_engine
mersenne_twister_engine
subtract_with_carry_engine
引擎適配器
discard_block_engine
independent_bits_engine
shuffle_order_engine
帶參數的引擎適配器
minstd_rand
minstd_rand0
mt19937
mt19937_64
ranlux24_base
ranlux48_base
ranlux24
ranlux48
knuth_b
default_random_engine //唯一不同平臺上生成的數列相同的引擎
2.2 引擎的成員函數
engine e; 默認構造
engine e(s); 以s的狀態建立一個引擎
engine e(e2); 複製構造
e.seed(); 將引擎設置爲初始狀態
e.seed(s); 將引擎設置爲s的狀態
e(); 返回一個隨機值
e.discard(n); 前進n個狀態
e1 == e2; 狀態是否相等
e1 != e2; 狀態是否不相等
os << e; 將e的狀態寫入output stream os
is >> e; 從input stream is 中讀取一個狀態放入e
2.3 測試
#include <iostream>
#include <random>
#include <algorithm>
#include <vector>
#include <sstream>
using namespace std;
int main()
{
//引擎:隨機性的源頭。
default_random_engine dre;
//整數。產生的隨機數分佈在[10,20]
//參數1可以默認爲0,參數2最大值爲 numeric_limits<type>::max()類型的最大值
uniform_int_distribution<int> di(10, 20);
for ( int i = 0; i < 20; ++i)
{
cout << di(dre) << ends;
}
cout << endl;
//浮點型。數值分佈在[10.0,20.0)區間
uniform_real_distribution<double> dr(10, 20);
for ( int i = 0; i < 8; ++i)
{
cout << dr(dre) << ends;
}
cout << endl;
//測試保存流狀態
stringstream state;
state << dre;
for ( int i = 0; i < 3; ++i)
{
cout << dr(dre) << ends;
}
cout << endl;
state >> dre;
for (int i = 0; i < 3; ++i)
{
cout << dr(dre) << ends;
}
cout << endl;
vector<int> myVec = { 1, 2, 3, 4, 5, 6, 7, 8 };
//洗牌。利用default_random_engine重新排列元素
shuffle(myVec.begin(), myVec.end(), dre);
for ( auto x : myVec)
{
cout << x << ends;
}
cout << endl;
//同一時間創建的引擎,隨機方式是相同的
//shuffle(myVec.begin(), myVec.end(), default_random_engine());
system("pause");
}
//16 13 20 19 14 17 10 16 15 14 12 13 13 18 10 15 12 13 12 17
//17.2584 19.8111 11.0986 17.9811 12.9703 10.0478 11.1246 16.3976
//18.7843 15.0366 17.9793
//18.7843 15.0366 17.9793
//7 5 1 8 4 2 3 6
//請按任意鍵繼續. . .
3、分佈
3.1 分佈的種類
均勻分佈
uniform_int_distribution 整型
uniform_real_distribution 浮點型
伯努利分佈
bernoulli_distribution 布爾
binomial_distribution 整型
geometric_distribution 整型
negative_binomial_distribution 整型
泊松分佈
poisson_distribution 整型
exponential_distribution 浮點型
gamma_distribution 浮點型
weibull_distribution 浮點型
extreme_value_distribution 浮點型
正態分佈
normal_distribution 浮點型
lognormal_distribution 浮點型
chi_squared_distribution 浮點型
cauchy_distribution 浮點型
fisher_f_distribution 浮點型
student_t_distribution 浮點型
抽樣分佈
discrete_distribution 整型
piecewise_constant_distribution浮點型
piecewise_linear_distribution 浮點型
3.2 分佈的成員函數
distr::result_type; 類型
distr d; 默認構造
distr d(args); 以args爲參數,創建分佈
d(e); 返回隨機值。推進引擎隨機分佈d區域中的值
d.min(); 返回最小值
d.max(); 返回最大值
d1 == d2; 判斷 狀態是否相等
d1 != d2; 判斷,狀態是否不相等
os << d; 將d的狀態寫入輸出流
is >> d; 從輸入流中讀取狀態給d
distr::param_type; 參數類型
distr d(pt); 創建一個分佈,以param_type pt完成參數化
d.param(); 返回當前參數化類型
d(e,pt); 根據 引擎 和 參數類型,返回下一個值,並推進引擎
d.param; 返回參數param的值
3.3 測試
#include <iostream>
#include <random>
#include <algorithm>
#include <string>
using namespace std;
template <typename Distr, typename Eng>
void distr(Distr d, Eng e, const string & name)
{
cout << name << ":" << endl;
cout << "min() = " << d.min() << endl;
cout << "max() = " << d.max() << endl;
cout << "values : " << d(e) << ends
<< d(e) << ends
<< d(e) << ends
<< d(e) << endl;
}
int main()
{
knuth_b e;
uniform_real_distribution<> ud(0, 10);
distr(ud, e, "uniform_real_distribution");
normal_distribution<> nd;
distr(nd, e, "normal_distribution");
exponential_distribution<> ed;
distr(ed, e, "normal_distribution");
gamma_distribution<> gd;
distr(gd, e, "normal_distribution");
system("pause");
}
//uniform_real_distribution:
//min() = 0
//max() = 10
//values : 8.30965 1.30427 9.47764 3.83416
//normal_distribution :
//min() = 4.94066e-324
//max() = 1.79769e+308
//values : 0.117963 - 0.131724 0.538967 - 0.140331
//normal_distribution :
//min() = 0
//max() = 1.79769e+308
//values : 1.77765 0.139753 2.95199 0.48356
//normal_distribution :
//min() = 4.94066e-324
//max() = 1.79769e+308
//values : 1.77765 0.139753 2.95199 0.48356
//請按任意鍵繼續. . .