discrete_distribution 클래스

각 간격의 확률이 균일하고 폭이 균등한 간격이 있는 이산 정수 분포를 생성합니다.

구문

template<class IntType = int>
class discrete_distribution
   {
public:
   // types
   typedef IntType result_type;
   struct param_type;

   // constructor and reset functions
   discrete_distribution();
   template <class InputIterator>
   discrete_distribution(InputIterator firstW, InputIterator lastW);
   discrete_distribution(initializer_list<double> weightlist);
   template <class UnaryOperation>
   discrete_distribution(size_t count, double xmin, double xmax, UnaryOperation funcweight);
   explicit discrete_distribution(const param_type& parm);
   void reset();

   // generating functions
   template <class URNG>
   result_type operator()(URNG& gen);
   template <class URNG>
   result_type operator()(URNG& gen, const param_type& parm);

   // property functions
   vector<double> probabilities() const;
   param_type param() const;
   void param(const param_type& parm);
   result_type min() const;
   result_type max() const;
   };

매개 변수

IntType
정수 결과 형식으로, 기본적으로 int로 지정되어 있습니다. 가능한 형식은 임>의 형식을 참조하세요<.

설명

이 표본 분포에는 각 간격의 확률이 균일하고 폭이 균등한 간격이 있습니다. 샘플링 분포에 대한 자세한 내용은 piecewise_linear_distribution 클래스piecewise_constant_distribution 클래스를 참조하세요.

다음 테이블은 개별 멤버에 대한 문서와 연결되어 있습니다.

discrete_distribution
param_type

속성 함수 vector<double> probabilities()는 생성된 각 정수에 대한 개별 확률을 반환합니다.

배포 클래스 및 해당 멤버에 대한 자세한 내용은 임>의 클래스를 참조<하세요.

예시

// compile with: /EHsc /W4
#include <random>
#include <iostream>
#include <iomanip>
#include <string>
#include <map>

using namespace std;

void test(const int s) {

    // uncomment to use a non-deterministic generator
    // random_device rd;
    // mt19937 gen(rd());
    mt19937 gen(1701);

    discrete_distribution<> distr({ 1, 2, 3, 4, 5 });

    cout << endl;
    cout << "min() == " << distr.min() << endl;
    cout << "max() == " << distr.max() << endl;
    cout << "probabilities (value: probability):" << endl;
    vector<double> p = distr.probabilities();
    int counter = 0;
    for (const auto& n : p) {
        cout << fixed << setw(11) << counter << ": " << setw(14) << setprecision(10) << n << endl;
        ++counter;
    }
    cout << endl;

    // generate the distribution as a histogram
    map<int, int> histogram;
    for (int i = 0; i < s; ++i) {
        ++histogram[distr(gen)];
    }

    // print results
    cout << "Distribution for " << s << " samples:" << endl;
    for (const auto& elem : histogram) {
        cout << setw(5) << elem.first << ' ' << string(elem.second, ':') << endl;
    }
    cout << endl;
}

int main()
{
    int samples = 100;

    cout << "Use CTRL-Z to bypass data entry and run using default values." << endl;
    cout << "Enter an integer value for the sample count: ";
    cin >> samples;

    test(samples);
}
Use CTRL-Z to bypass data entry and run using default values.
Enter an integer value for the sample count: 100
min() == 0
max() == 4
probabilities (value: probability):
          0:   0.0666666667
          1:   0.1333333333
          2:   0.2000000000
          3:   0.2666666667
          4:   0.3333333333

Distribution for 100 samples:
    0 :::
    1 ::::::::::::::
    2 ::::::::::::::::::
    3 :::::::::::::::::::::::::::::
    4 ::::::::::::::::::::::::::::::::::::

요구 사항

헤더:<random>

네임스페이스: std

discrete_distribution::discrete_distribution

분포를 생성합니다.

// default constructor
discrete_distribution();

// construct using a range of weights, [firstW, lastW)
template <class InputIterator>
discrete_distribution(InputIterator firstW, InputIterator lastW);

// construct using an initializer list for range of weights
discrete_distribution(initializer_list<double> weightlist);

// construct using unary operation function
template <class UnaryOperation>
discrete_distribution(size_t count, double low, double high, UnaryOperation weightfunc);

// construct from an existing param_type structure
explicit discrete_distribution(const param_type& parm);

매개 변수

firstW
분포를 생성할 목록의 첫 번째 반복기입니다.

lastW
분포를 생성할 목록의 마지막 반복기입니다(반복기는 끝에 빈 요소를 사용하기 때문에 제외됨).

weightlist
분포를 생성할 initializer_list입니다.

count
분포 범위의 요소 수입니다. count==0이면 기본 생성자와 동일합니다(항상 0 생성).

low
분포 범위의 가장 작은 값입니다.

high
분포 범위의 가장 큰 값입니다.

weightfunc
분포의 확률 함수를 나타내는 개체입니다. 매개 변수와 반환 값은 둘 다 double로 변환할 수 있어야 합니다.

parm
분포를 생성하는 데 사용되는 param_type 구조체입니다.

설명

기본 생성자는 저장된 확률 값에 값이 1인 요소가 있는 개체를 생성합니다. 그러면 0을 항상 생성하는 분포가 됩니다.

firstWlastW 매개 변수가 포함된 반복기 범위 생성자는 간격 시퀀스 [firstW, lastW)에 대해 반복기에서 가져온 가중치 값을 사용하여 분포 개체를 생성합니다.

weightlist 매개 변수가 있는 이니셜라이저 목록 생성자는 이니셜라이저 목록 가중치 목록의 가중치를 사용하여 배포 개체를 생성합니다.

count, low, highweightfunc 매개 변수가 포함된 생성자는 다음 규칙에 따라 초기화된 분포 개체를 생성합니다.

  • count< 1, n = 1이면 항상 0을 생성하는 기본 생성자와 동일합니다.
  • 개수 0인 경우 n = 개수입니다.> 제공된 d = (high - low) / n이 0보다 크면 d 균일 하위 범위에서 각 가중치는 다음과 weight[k] = weightfunc(x)같이 할당됩니다. 여기서 x = low + k * d d + / 2, for k = 0, ..., n - 1.

param_type 매개 변수 parm이 포함된 생성자는 parm을 사용하여 분포 개체를 저장된 매개 변수 구조체로 생성합니다.

discrete_distribution::param_type

분포의 모든 매개 변수를 저장합니다.

struct param_type {
   typedef discrete_distribution<result_type> distribution_type;
   param_type();

   // construct using a range of weights, [firstW, lastW)
   template <class InputIterator>
   param_type(InputIterator firstW, InputIterator lastW);

   // construct using an initializer list for range of weights
   param_type(initializer_list<double> weightlist);

   // construct using unary operation function
   template <class UnaryOperation>
   param_type(size_t count, double low, double high, UnaryOperation weightfunc);

   std::vector<double> probabilities() const;

   bool operator==(const param_type& right) const;
   bool operator!=(const param_type& right) const;
   };

매개 변수

firstW
분포를 생성할 목록의 첫 번째 반복기입니다.

lastW
분포를 생성할 목록의 마지막 반복기입니다(반복기는 끝에 빈 요소를 사용하기 때문에 제외됨).

weightlist
분포를 생성할 initializer_list입니다.

count
분포 범위의 요소 수입니다. count가 0이면 기본 생성자와 같습니다(항상 0 생성).

low
분포 범위의 가장 작은 값입니다.

high
분포 범위의 가장 큰 값입니다.

weightfunc
분포의 확률 함수를 나타내는 개체입니다. 매개 변수와 반환 값은 둘 다 double로 변환할 수 있어야 합니다.

right
이 매개 변수와 비교할 param_type 개체입니다.

설명

이 매개 변수 패키지를 operator()에 전달하여 반환 값을 생성할 수 있습니다.

참고 항목

<random>