random_bernoulli_distribution
random_bernoulli_distribution (베르누이 분포)
std::bernoulli_distribution은 이산 확률 함수에 따라 임의의 불리언 값을 생성하는 분포 클래스예요. true가 나올 확률은 p예요. C++11부터 사용할 수 있어요.
출처: cppreference
본문
<random> 헤더에 정의되어 있고, 시그니처는 다음과 같아요.
class bernoulli_distribution;
(since C++11)
이산 확률 함수에 따라 임의의 불리언 값을 생성해요. true의 확률은 다음과 같아요.
P(b|p) = { p, if b is true; 1-p, if b is false }
std::bernoulli_distribution은 RandomNumberDistribution을 만족해요.
멤버 타입 (Member types)
| 멤버 타입 | 정의 |
|---|---|
| result_type (C++11) | bool |
| param_type | 매개변수 집합의 타입, RandomNumberDistribution 참고 |
멤버 함수 (Member functions)
- (constructor) — 새 분포를 생성해요 (공개 멤버 함수)
- reset — 분포의 내부 상태를 재설정 (공개 멤버 함수)
- 생성: operator() — 분포에서 다음 난수를 생성
- 특성: p (C++11) — p 분포 매개변수(true 생성 확률) 반환; param — 분포 매개변수 객체 얻기/설정; min — 생성될 수 있는 최솟값; max — 생성될 수 있는 최댓값
비멤버 함수 (Non-member functions)
- operator== operator!= (C++11, C++20에서 제거) — 두 분포 객체 비교 (함수)
- operator<< operator>> (C++11) — 의사난수 분포에 스트림 입출력 수행 (함수 템플릿)
예제 (Example)
이 코드를 실행해 봐요.
#include <iomanip>
#include <iostream>
#include <map>
#include <random>
#include <string>
int main()
{
std::random_device rd;
std::mt19937 gen(rd());
std::bernoulli_distribution d(0.25); // 25% true
std::map<bool, int> hist;
for (int n = 0; n != 10000; ++n)
++hist[d(gen)];
for (auto [b, count] : hist)
std::cout << (b ? "true" : "false") << ": " << count << '\n';
}