std:: piecewise_constant_distribution
From cppreference.net
|
定義先ヘッダ
<random>
|
||
|
template
<
class
RealType
=
double
>
class piecewise_constant_distribution ; |
(C++11以降) | |
std::piecewise_constant_distribution
は、複数の部分区間
[b
i
, b
i+1
)
のそれぞれにおいて一様分布するランダムな浮動小数点数を生成します。各区間は独自の重み
w
i
を持ちます。区間の境界の集合と重みの集合がこの分布のパラメータとなります。
| w i |
| S (b i+1 - b i ) |
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)
(removed in C++20)
|
二つの分布オブジェクトを比較する
(関数) |
|
(C++11)
|
擬似乱数分布に対するストリーム入出力を実行する
(関数テンプレート) |
例
このコードを実行
#include <iostream> #include <map> #include <random> #include <string> int main() { std::random_device rd; std::mt19937 gen(rd()); // 50%の確率で0から1の間の乱数を生成 // 50%の確率で10から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)