Namespaces
Variants

std:: uniform_int_distribution

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

닫힌 구간 [a, b] 에서 균일하게 분포된, 즉 이산 확률 함수에 따라 분포된 임의의 정수 값 i 을 생성합니다.

P(i|a,b) =
1
b − a + 1
.

std::uniform_int_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)
분포 매개변수를 반환함
(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)
두 분포 객체를 비교함
(함수)
의사 난수 분포에 대한 스트림 입출력을 수행함
(함수 템플릿)

예제

이 프로그램은 6면체 주사위 를 던지는 것을 시뮬레이션합니다.

#include <iostream>
#include <random>
int main()
{
    std::random_device rd;  // a seed source for the random number engine
    std::mt19937 gen(rd()); // mersenne_twister_engine seeded with rd()
    std::uniform_int_distribution<> distrib(1, 6);
    // Use distrib to transform the random unsigned int
    // generated by gen into an int in [1, 6]
    for (int n = 0; n != 10; ++n)
        std::cout << distrib(gen) << ' ';
    std::cout << '\n';
}

가능한 출력:

1 1 6 5 2 2 5 5 6 2