Namespaces
Variants

std:: bernoulli_distribution

From cppreference.net
헤더 파일에 정의됨 <random>
class bernoulli_distribution ;
(C++11 이후)

불리언 값을 이산 확률 함수에 따라 무작위로 생성합니다. true 의 확률은

P(b|p) =

p , 만약 b true
1 − p , 만약 b false

std::bernoulli_distribution RandomNumberDistribution 요구 사항을 만족합니다.

목차

멤버 타입

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

멤버 함수

새로운 분포를 생성합니다
(public member function)
(C++11)
분포의 내부 상태를 재설정합니다
(public member function)
생성
(C++11)
분포에서 다음 난수를 생성합니다
(public member function)
특성
(C++11)
p 분포 매개변수를 반환합니다 ( true 를 생성할 확률)
(public member function)
(C++11)
분포 매개변수 객체를 가져오거나 설정합니다
(public member function)
(C++11)
잠재적으로 생성될 수 있는 최소값을 반환합니다
(public member function)
(C++11)
잠재적으로 생성될 수 있는 최대값을 반환합니다
(public member function)

비멤버 함수

(C++11) (C++11) (removed in C++20)
두 분포 객체를 비교함
(함수)
의사 난수 분포에 대한 스트림 입출력을 수행함
(함수 템플릿)

예제

#include <iomanip>
#include <iostream>
#include <map>
#include <random>
#include <string>
int main()
{
    std::random_device rd;
    std::mt19937 gen(rd());
    // "true"를 1/4 확률로 반환
    // "false"를 3/4 확률로 반환
    std::bernoulli_distribution d(0.25);
    std::map<bool, int> hist;
    for (int n = 0; n < 10000; ++n)
        ++hist[d(gen)];
    std::cout << std::boolalpha;
    for (auto const& [key, value] : hist)
        std::cout << std::setw(5) << key << ' '
                  << std::string(value / 500, '*') << '\n';
}

가능한 출력:

false ***************
 true ****

외부 링크

Weisstein, Eric W. "Bernoulli Distribution." MathWorld — Wolfram 웹 리소스에서.