Namespaces
Variants

std:: geometric_distribution

From cppreference.net
헤더에 정의됨 <random>
template < class IntType = int >
class geometric_distribution ;
(C++11부터)

음수가 아닌 임의의 정수 값을 생성하며, i 는 이산 확률 함수에 따라 분포됩니다:

P(i|p) = p · (1 − p) i

이 값은 정확히 1번의 성공이 발생하기 전, 일련의 독립적인 예/아니오 시도(각 시도가 확률 p로 성공함)에서 발생한 실패 횟수를 나타냅니다.

std :: geometric_distribution <> ( p ) 는 정확히 std:: negative_binomial_distribution <> ( 1 , p ) 와 동등합니다. 또한 이는 std::exponential_distribution 의 이산형 상대에 해당합니다.

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

목차

템플릿 매개변수

IntType - 생성자가 생성하는 결과 타입. 이것이 다음 중 하나가 아닌 경우의 효과는 정의되지 않음: short , int , long , long long , unsigned short , unsigned int , unsigned long , 또는 unsigned long long .

멤버 타입

멤버 타입 정의
result_type (C++11) IntType
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) (C++20에서 제거됨)
두 분포 객체를 비교함
(함수)
의사 난수 분포에 대한 스트림 입출력을 수행함
(함수 템플릿)

예제

std :: geometric_distribution <> ( 0.5 ) 은 기본값이며 동전 던지기에서 앞면이 나올 때까지 필요한 던진 횟수를 나타냅니다.

#include <iomanip>
#include <iostream>
#include <map>
#include <random>
#include <string>
int main()
{
    std::random_device rd;
    std::mt19937 gen(rd());
    std::geometric_distribution<> d;
        // same as 
        // std::negative_binomial_distribution<> d(1, 0.5):
    std::map<int, int> hist;
    for (int n = 0; n != 10000; ++n)
        ++hist[d(gen)];
    for (auto [x, y] : hist)
    {
        const char c = x < 10 ? x + '0' : x - 10 + 'a';
        std::cout << c << ' ' << std::string(y / 100, '*') << '\n';
    }
}

가능한 출력:

0 *************************************************
1 *************************
2 ************
3 ******
4 **
5 *
6
7
8
9

외부 링크

Weisstein, Eric W. "기하 분포." MathWorld — Wolfram 웹 자원에서.