std::piecewise_linear_distribution
From 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) |
新しい分布を構築します。 (public member function) |
| (C++11) |
分布の内部状態をリセットします。 (public member function) |
生成 | |
| (C++11) |
分布における次の乱数を生成する (public member function) |
特性 | |
| (C++11) |
分布パラメータを返します。 (public member function) |
| (C++11) |
分布パラメータオブジェクトを取得または設定します。 (public member function) |
| (C++11) |
生成される可能性のある最小値を返します。 (public member function) |
| (C++11) |
生成される可能性のある最大値を返します。 (public member function) |
[編集] 非メンバ関数
| (C++11)(C++11)(C++20で削除) |
2つの分布オブジェクトを比較します。 (function) |
| (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 *