std::piecewise_constant_distribution
來自 cppreference.com
定義於標頭檔案 <random> |
||
template< class RealType = double > class piecewise_constant_distribution; |
(C++11 起) | |
std::piecewise_constant_distribution
產生隨機浮點數,這些浮點數在幾個子區間 [bi, bi+1) 內均勻分佈,每個子區間都有自己的權重 wi。區間邊界集和權重集是此分佈的引數。
wi |
S (bi+1 - bi) |
std::piecewise_constant_distribution
滿足 RandomNumberDistribution 的所有要求。
目錄 |
[編輯] 模板引數
RealType | - | 生成器生成的結果型別。如果這不是 float、double 或 long double 之一,則效果未定義。 |
[編輯] 成員型別
成員型別 | 定義 |
result_type (C++11) |
RealType |
param_type (C++11) |
引數集的型別,參見 RandomNumberDistribution。 |
[編輯] 成員函式
(C++11) |
構造新的分佈 (public member function) |
(C++11) |
重置分佈的內部狀態 (public member function) |
生成 | |
(C++11) |
生成分佈中的下一個隨機數 (public member function) |
特性 | |
返回分佈引數 (public member function) | |
(C++11) |
獲取或設定分佈引數物件 (public member function) |
(C++11) |
返回可能生成的最小值 (public member function) |
(C++11) |
返回可能生成的最大值 (public member function) |
[編輯] 非成員函式
(C++11起)(C++11起)(C++20中移除) |
比較兩個分佈物件 (函式) |
(C++11) |
對偽隨機數分佈執行流輸入和輸出 (函式模板) |
[編輯] 示例
執行此程式碼
#include <iostream> #include <map> #include <random> #include <string> int main() { std::random_device rd; std::mt19937 gen(rd()); // 50% of the time, generate a random number between 0 and 1 // 50% of the time, generate a random number between 10 and 15 std::vector<double> i {0, 1, 10, 15}; std::vector<double> w {1, 0, 1}; std::piecewise_constant_distribution<> d(i.begin(), i.end(), w.begin()); std::map<int, int> hist; for (int n {}; n < 1e4; ++n) ++hist[d(gen)]; for (std::cout << std::hex << std::uppercase; auto [x, y] : hist) std::cout << x << ' ' << std::string(y / 100, '*') << '\n'; }
可能的輸出
0 ************************************************** A ********** B ********* C ********* D ********** E *********
[編輯] 參考
- C++23 標準 (ISO/IEC 14882:2024)
- 28.5.9.6.2 類模板 piecewise_constant_distribution [rand.dist.samp.pconst] (p: 1421-1422)
- C++20 標準 (ISO/IEC 14882:2020)
- 29.6.9.6.2 類模板 piecewise_constant_distribution [rand.dist.samp.pconst] (p: 1207-1208)
- C++17 標準 (ISO/IEC 14882:2017)
- 29.6.8.6.2 類模板 piecewise_constant_distribution [rand.dist.samp.pconst] (p: 1098-1100)
- C++14 標準 (ISO/IEC 14882:2014)
- 26.5.8.6.2 類模板 piecewise_constant_distribution [rand.dist.samp.pconst] (p: 962-964)
- C++11 標準 (ISO/IEC 14882:2011)
- 26.5.8.6.2 類模板 piecewise_constant_distribution [rand.dist.samp.pconst] (p: 955-957)