名前空間
変種
操作

std::normal_distribution

From cppreference.com
< cpp‎ | numeric‎ | random
 
 
 
 
 
ヘッダー <random> で定義
template< class RealType = double >
class normal_distribution;
(C++11以降)

正規分布(またはガウス分布)に従う乱数を生成します。定義は以下の通りです。

f(x; μ,σ) =
1
σ
exp

-1
2


x-μ
σ


2


ここで、μ平均 であり、σ標準偏差 (stddev) です。

std::normal_distribution は、RandomNumberDistribution のすべての要件を満たします。

目次

[編集] テンプレートパラメータ

RealType - ジェネレータによって生成される結果の型。これが float, double, または long double のいずれでもない場合、動作は未定義です。

[編集] メンバ型

メンバ型 定義
result_type (C++11) RealType
param_type (C++11) パラメータセットの型。RandomNumberDistribution を参照してください。

[編集] メンバ関数

新しい分布を構築します。
(public member function) [edit]
(C++11)
分布の内部状態をリセットします。
(public member function) [edit]
生成
分布における次の乱数を生成する
(public member function) [edit]
特性
分布パラメータを返します。
(public member function) [edit]
(C++11)
分布パラメータオブジェクトを取得または設定します。
(public member function) [edit]
(C++11)
生成される可能性のある最小値を返します。
(public member function) [edit]
(C++11)
生成される可能性のある最大値を返します。
(public member function) [edit]

[編集] 非メンバ関数

(C++11)(C++11)(C++20で削除)
2つの分布オブジェクトを比較します。
(function) [edit]
疑似乱数分布のストリーム入出力を実行
(関数テンプレート) [編集]

[編集]

#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <random>
#include <string>
 
int main()
{
    std::random_device rd{};
    std::mt19937 gen{rd()};
 
    // Values near the mean are the most likely. Standard deviation
    // affects the dispersion of generated values from the mean.
    std::normal_distribution d{5.0, 2.0};
 
    // Draw a sample from the normal distribution and round it to an integer.
    auto random_int = [&d, &gen]{ return std::lround(d(gen)); };
 
    std::map<long, unsigned> histogram{};
    for (auto n{10000}; n; --n)
        ++histogram[random_int()];
 
    for (const auto [k, v] : histogram)
        std::cout << std::setw(2) << k << ' ' << std::string(v / 200, '*') << '\n';
}

実行結果の例

-1
 0
 1 *
 2 ***
 3 *****
 4 ********
 5 *********
 6 *********
 7 ******
 8 ***
 9 *
10
11

[編集] 外部リンク

1.  Weisstein, Eric W. "Normal Distribution." MathWorld — A Wolfram Web Resourceより。
2.  正規分布 — Wikipediaより。
English 日本語 中文(简体) 中文(繁體)