◐ Shell
clean mode source ↗

std::normal_distribution - cppreference.com

提供: cppreference.com

<tbody> </tbody>

template< class RealType = double > class normal_distribution;

(C++11以上)

正規 (ガウス) 分布に従った乱数を生成します。 以下のように定義されます。

f(x; μ,σ) = exp





2


ここで μ平均σ標準偏差です。

std::normal_distributionRandomNumberDistribution の要件をすべて満たします。

テンプレート引数

RealType - ジェネレータが生成する結果の型。 floatdouble または long double のいずれかでない場合、効果は未定義です

メンバ型

メンバ関数

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

非メンバ関数

#include <iostream>
#include <iomanip>
#include <string>
#include <map>
#include <random>
#include <cmath>
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,2};

    std::map<int, int> hist{};
    for(int n=0; n<10000; ++n) {
        ++hist[std::round(d(gen))];
    }
    for(auto p : hist) {
        std::cout << std::setw(2)
                  << p.first << ' ' << std::string(p.second/200, '*') << '\n';
    }
}

出力例:

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

外部リンク