std:: geometric_distribution
|
ヘッダーで定義
<random>
|
||
|
template
<
class
IntType
=
int
>
class geometric_distribution ; |
(C++11以降) | |
ランダムな非負整数値 i を生成し、離散確率関数に従って分布させます:
-
P(i|p) = p · (1 − p)
i
この値は、独立した成功/失敗の試行(各試行が確率pで成功する)において、ちょうど1回の成功が発生するまでの失敗の回数を表します。
std :: geometric_distribution <> ( p ) は std:: negative_binomial_distribution <> ( 1 , p ) と完全に等価です。また、これは std::exponential_distribution の離散版でもあります。
std::geometric_distribution
は
RandomNumberDistribution
を満たす。
目次 |
テンプレートパラメータ
| IntType | - | ジェネレータによって生成される結果の型。これが short 、 int 、 long 、 long long 、 unsigned short 、 unsigned int 、 unsigned long 、または unsigned long long のいずれでもない場合、動作は未定義です。 |
メンバー型
| メンバー型 | 定義 |
result_type
(C++11)
|
IntType |
param_type
(C++11)
|
パラメータセットの型、 RandomNumberDistribution を参照。 |
メンバー関数
|
(C++11)
|
新しい分布を構築する
(public member function) |
|
(C++11)
|
分布の内部状態をリセットする
(public member function) |
生成 |
|
|
(C++11)
|
分布内の次の乱数を生成する
(public member function) |
特性 |
|
|
(C++11)
|
p
分布パラメータ(試行が
true
を生成する確率)を返す
(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)
|
擬似乱数分布に対するストリーム入出力を実行する
(関数テンプレート) |
例
std :: geometric_distribution <> ( 0.5 ) はデフォルト設定であり、表が出るまでに必要なコイントスの回数を表します。
#include <iomanip> #include <iostream> #include <map> #include <random> #include <string> int main() { std::random_device rd; std::mt19937 gen(rd()); std::geometric_distribution<> d; // same as // std::negative_binomial_distribution<> d(1, 0.5): std::map<int, int> hist; for (int n = 0; n != 10000; ++n) ++hist[d(gen)]; for (auto [x, y] : hist) { const char c = x < 10 ? x + '0' : x - 10 + 'a'; std::cout << c << ' ' << std::string(y / 100, '*') << '\n'; } }
出力例:
0 ************************************************* 1 ************************* 2 ************ 3 ****** 4 ** 5 * 6 7 8 9
外部リンク
| Weisstein, Eric W. "Geometric Distribution." From MathWorld — A Wolfram Web Resource. |
| Weisstein, Eric W. "幾何分布" MathWorld — Wolfram Web リソースより |