std::piecewise_linear_distribution
來自 cppreference.com
定義於標頭檔案 <random> |
||
template< class RealType = double > class piecewise_linear_distribution; |
(C++11 起) | |
std::piecewise_linear_distribution
生成隨機浮點數,這些浮點數根據幾個子區間 [bi, bi+1) 內的線性機率密度函式分佈。該分佈使得每個區間邊界處的機率密度都精確等於預定義值 pi。
bi+1-x |
bi+1-bi |
x-bi |
bi+1-bi |
1 |
2 |
區間邊界集 bi 和邊界處的權重集 wi 是此分佈的引數。
std::piecewise_linear_distribution
滿足 RandomNumberDistribution 的所有要求。
目錄 |
[編輯] 模板引數
RealType | - | 生成器生成的返回型別。如果它不是 float、double 或 long double 之一,則效果未定義。 |
[編輯] 成員型別
成員型別 | 定義 |
result_type (C++11) |
RealType |
param_type (C++11) |
引數集的型別,參見 RandomNumberDistribution。 |
[編輯] 成員函式
(C++11) |
構造新的分佈 (公有成員函式) |
(C++11) |
重置分佈的內部狀態 (公有成員函式) |
生成 | |
(C++11) |
生成分佈中的下一個隨機數 (公有成員函式) |
特性 | |
(C++11) |
返回分佈引數 (公有成員函式) |
(C++11) |
獲取或設定分佈引數物件 (公有成員函式) |
(C++11) |
返回可能生成的最小值 (公有成員函式) |
(C++11) |
返回可能生成的最大值 (公有成員函式) |
[編輯] 非成員函式
(C++11起)(C++11起)(C++20中移除) |
比較兩個分佈物件 (函式) |
(C++11) |
對偽隨機數分佈執行流輸入和輸出 (函式模板) |
[編輯] 示例
執行此程式碼
#include <iomanip> #include <iostream> #include <map> #include <random> #include <string> int main() { std::random_device rd; std::mt19937 gen{rd()}; // increase the probability from 0 to 5 // remain flat from 5 to 10 // decrease from 10 to 15 at the same rate std::vector<double> i{0, 5, 10, 15}; std::vector<double> w{0, 1, 1, 0}; std::piecewise_linear_distribution<> d{i.begin(), i.end(), w.begin()}; std::map<int, int> hist; for (int n{}; n < 1e4; ++n) ++hist[d(gen)]; for (auto [x, y] : hist) std::cout << std::setw(2) << std::setfill('0') << x << ' ' << std::string(y / 100, '*') << '\n'; }
可能的輸出
00 * 01 *** 02 **** 03 ****** 04 ********* 05 ********* 06 ********* 07 ********** 08 ********* 09 ********** 10 ********* 11 ******* 12 **** 13 *** 14 *