Namespaces
Variants

std:: normal_distribution

From cppreference.net
std::normal_distribution
헤더 파일에 정의됨 <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)
(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) (C++20에서 제거됨)
두 분포 객체를 비교함
(함수)
의사 난수 분포에 대한 스트림 입출력을 수행함
(함수 템플릿)

예제

#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <random>
#include <string>
int main()
{
    std::random_device rd{};
    std::mt19937 gen{rd()};
    // 평균 근처 값이 가장 많이 생성됩니다. 표준 편차는
    // 생성된 값들이 평균으로부터 퍼져나가는 정도에 영향을 줍니다.
    std::normal_distribution d{5.0, 2.0};
    // 정규 분포에서 샘플을 추출하여 정수로 반올림합니다.
    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 — 울프램 웹 리소스에서.
2. Normal distribution — 위키백과에서.