poisson_distribution クラス

ポワソン分布を生成します。

構文

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

   // constructors and reset functions
   explicit poisson_distribution(double mean = 1.0);
   explicit poisson_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
   double mean() const;
   param_type param() const;
   void param(const param_type& parm);
   result_type min() const;
   result_type max() const;
   };

パラメーター

IntType
結果を表す整数型。既定値は int です。 使用可能な型については、「<random>」を参照してください。

解説

このクラス テンプレートは、ポワソン分布を使用してユーザー指定の整数型の値を生成する分布を表します。 次の表は、個々のメンバーに関する記事にリンクしています。

poisson_distribution
param_type

プロパティ関数 mean() は、格納されている分布パラメーター mean の値を返します。

プロパティ メンバー param() は、格納されている分布パラメーター パッケージ param_type を設定または返します。

メンバー関数の min()max() はそれぞれ、考えられる結果の最小値と最大値を返します。

reset() メンバー関数は、次回 operator() を呼び出したときに、その結果が、その前にエンジンから取得された値に左右されないようにするため、キャッシュされている値をすべて破棄します。

operator() メンバー関数は、現在のパラメーター パッケージと指定したパラメーター パッケージのいずれかから、URNG エンジンに基づいて次に生成された値を返します。

分布クラスとそのメンバーの詳細については、<random> をご覧ください。

ポワソン分布の詳細については、Wolfram MathWorld の記事「ポワソン分布」をご覧ください。

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

void test(const double p, const int s) {

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

    std::poisson_distribution<> distr(p);

    std::cout << std::endl;
    std::cout << "min() == " << distr.min() << std::endl;
    std::cout << "max() == " << distr.max() << std::endl;
    std::cout << "p() == " << std::fixed << std::setw(11) << std::setprecision(10) << distr.mean() << std::endl;

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

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

int main()
{
    double p_dist = 1.0;

    int samples = 100;

    std::cout << "Use CTRL-Z to bypass data entry and run using default values." << std::endl;
    std::cout << "Enter a floating point value for the 'mean' distribution parameter (must be greater than zero): ";
    std::cin >> p_dist;
    std::cout << "Enter an integer value for the sample count: ";
    std::cin >> samples;

    test(p_dist, samples);
}

最初のテスト:

Use CTRL-Z to bypass data entry and run using default values.
Enter a floating point value for the 'mean' distribution parameter (must be greater than zero): 1
Enter an integer value for the sample count: 100
min() == 0
max() == 2147483647
p() == 1.0000000000
Distribution for 100 samples:
    0 ::::::::::::::::::::::::::::::
    1 ::::::::::::::::::::::::::::::::::::::
    2 :::::::::::::::::::::::
    3 ::::::::
    5 :

2 回目のテスト:

Use CTRL-Z to bypass data entry and run using default values.
Enter a floating point value for the 'mean' distribution parameter (must be greater than zero): 10
Enter an integer value for the sample count: 100
min() == 0
max() == 2147483647
p() == 10.0000000000
Distribution for 100 samples:
    3 :
    4 ::
    5 ::
    6 ::::::::
    7 ::::
    8 ::::::::
    9 ::::::::::::::
   10 ::::::::::::
   11 ::::::::::::::::
   12 :::::::::::::::
   13 ::::::::
   14 ::::::
   15 :
   16 ::
   17 :

要件

ヘッダー: <random>

名前空間: std

poisson_distribution::poisson_distribution

分布を作成します。

explicit poisson_distribution(RealType mean = 1.0);
explicit binomial_distribution(const param_type& parm);

パラメーター

mean
mean 分布パラメーター。

parm
分布の作成に使用されるパラメーターの構造体。

解説

前提条件: 0.0 < mean

1 つ目のコンストラクターは、格納されている mean の値が mean の値を保持するオブジェクトを作成します。

2 つ目のコンストラクターは、格納パラメーターが parm から初期化されるオブジェクトを作成します。 param() メンバー関数を呼び出すと、既存の分布の現在のパラメーターを取得および設定できます。

poisson_distribution::param_type

分布のパラメーターを格納します。

struct param_type {
   typedef poisson_distribution<IntType> distribution_type;
   param_type(double mean = 1.0);
   double mean() const;

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

パラメーター

poisson_distribution のコンストラクター パラメーターをご覧ください。

解説

前提条件: 0.0 < mean

この構造体は、インスタンス化時に分布のクラス コンストラクターに渡したり、param() メンバー関数に渡して、既存の分布の格納されているパラメーターを設定したり、operator() に渡して、格納されているパラメーターの代わりに使用したりすることができます。

関連項目

<random>