random_exponential_distribution
random_exponential_distribution (지수 분포)
std::exponential_distribution은 확률 밀도 함수에 따라 분포된 임의의 음이 아닌 부동소수점 값 x를 생성하는 분포 클래스예요. C++11부터 사용할 수 있어요.
출처: cppreference
본문
<random> 헤더에 정의되어 있고, 시그니처는 다음과 같아요.
template< class RealType = double >
class exponential_distribution;
(since C++11)
확률 밀도 함수에 따라 분포된 임의의 음이 아닌 부동소수점 값 x를 생성해요:
P(x|λ) = λe^(-λx)
얻은 값은 무작위 사건이 시간/거리 단위당 일정한 속도 λ로 발생한다면 다음 무작위 사건까지의 시간/거리예요. 예를 들어 이 분포는 가이거 계수기의 클릭 사이의 시간이나 DNA 가닥의 점 돌연변이 사이의 거리를 설명해요.
이것은 std::geometric_distribution의 연속형 짝이에요.
std::exponential_distribution은 RandomNumberDistribution을 만족해요.
템플릿 매개변수 (Template parameters)
- RealType — 결과 타입. float, double, long double 중 하나가 아니면 효과는 정의되지 않아요.
멤버 타입 (Member types)
| 멤버 타입 | 정의 |
|---|---|
| result_type (C++11) | RealType |
| param_type | 매개변수 집합의 타입 |
멤버 함수 (Member functions)
- (constructor) — 새 분포 생성
- reset — 내부 상태 재설정
- 생성: operator() — 다음 난수 생성
- 특성: lambda (C++11) — λ 분포 매개변수 반환; param — 매개변수 객체 얻기/설정; min — 최솟값; max — 최댓값
비멤버 함수 (Non-member functions)
- operator== operator!= — 두 분포 객체 비교
- operator<< operator>> (C++11) — 스트림 입출력
예제 (Example)
이 코드를 실행해 봐요.
#include <iostream>
#include <map>
#include <random>
int main()
{
std::random_device rd;
std::mt19937 gen(rd());
std::exponential_distribution<> d(0.5); // λ = 0.5
std::map<int, int> hist;
for (int n = 0; n != 10000; ++n)
++hist[std::round(d(gen))];
for (auto [x, count] : hist)
if (x < 20)
std::cout << x << ": " << std::string(count / 200, '*') << '\n';
}