random_cauchy_distribution

random_cauchy_distribution (코시 분포)

std::cauchy_distribution은 코시 분포(로런츠 분포라고도 불러요)에 따라 임의의 값을 생성하는 분포 클래스예요. C++11부터 사용할 수 있어요.

출처: cppreference

본문

<random> 헤더에 정의되어 있고, 시그니처는 다음과 같아요.

template< class RealType = double >
class cauchy_distribution;

(since C++11)

코시 분포(로런츠 분포라고도 불러요)에 따라 임의의 값을 생성해요:

f(x; a, b) = (bπ[1 + ((x-a)/b)²])⁻¹

std::cauchy_distribution은 RandomNumberDistribution의 모든 요구사항을 만족해요.

템플릿 매개변수 (Template parameters)

  • RealType — 생성기가 생성하는 결과 타입. float, double, long double 중 하나가 아니면 효과는 정의되지 않아요.

멤버 타입 (Member types)

멤버 타입 정의
result_type (C++11) RealType
param_type 매개변수 집합의 타입, RandomNumberDistribution 참고

멤버 함수 (Member functions)

  • (constructor) — 새 분포를 생성해요
  • reset — 내부 상태 재설정
  • 생성: operator() — 다음 난수 생성
  • 특성: a, b — 분포 매개변수 반환; param — 매개변수 객체 얻기/설정; min — 최솟값; max — 최댓값

비멤버 함수 (Non-member functions)

  • operator== operator!= (C++11, C++20에서 제거) — 두 분포 객체 비교
  • operator<< operator>> (C++11) — 스트림 입출력

예제 (Example)

이 코드를 실행해 봐요.

#include <iostream>
#include <map>
#include <random>

int main()
{
    std::random_device rd;
    std::mt19937 gen(rd());
    std::cauchy_distribution<> d(0.0, 1.0);

    std::map<int, int> hist;
    for (int n = 0; n != 10000; ++n)
        ++hist[std::round(d(gen))];

    for (auto [x, count] : hist)
        if (x > -10 && x < 10)
            std::cout << x << ": " << std::string(count / 100, '*') << '\n';
}

더 알아보기 (Learn more)

cppreference