Share via


uniform_int_distribution 類別

在輸出範圍 (內含-內含) 內,產生統一 (每個值的可能性相等) 整數分佈。

語法

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

   // constructors and reset functions
   explicit uniform_int_distribution(
      result_type a = 0, result_type b = numeric_limits<result_type>::max());
   explicit uniform_int_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
   result_type a() const;
   result_type b() const;
   param_type param() const;
   void param(const param_type& parm);
   result_type min() const;
   result_type max() const;
};

參數

IntType
整數結果類型,預設值為 int。 如需可能的類型,請參閱 < 隨機 >

備註

類別範本描述包容性分佈,其會產生具有分佈的使用者指定整數型別值,讓每個值都一樣可能。 下表提供各個成員的文章連結。

uniform_int_distribution
param_type

屬性成員 a() 會傳回目前儲存的分佈下限,而 b() 會傳回目前儲存的上限。 對於此分佈類別,這些下限和上限值和一般屬性函式 min()max() 所傳回的一樣。

屬性成員 param() 會設定或傳回 param_type 預存分佈參數套件。

min()max() 成員函式會分別傳回最小可能結果和最大可能結果。

reset() 成員函式會捨棄任何快取的值,讓下個針對 operator() 呼叫的結果不是取決於呼叫之前取自引擎的任何值。

operator() 成員函式會根據 URNG 引擎傳回下一個產生的值,無論是從目前的參數封裝或是指定的參數封裝。

如需散發類別及其成員的詳細資訊,請參閱 < 隨機 >

範例

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

void test(const int a, const int b, const int s) {

    // uncomment to use a non-deterministic seed
    //    std::random_device rd;
    //    std::mt19937 gen(rd());
    std::mt19937 gen(1729);

    std::uniform_int_distribution<> distr(a, b);

    std::cout << "lower bound == " << distr.a() << std::endl;
    std::cout << "upper bound == " << distr.b() << 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()
{
    int a_dist = 1;
    int b_dist = 10;

    int samples = 100;

    std::cout << "Use CTRL-Z to bypass data entry and run using default values." << std::endl;
    std::cout << "Enter an integer value for the lower bound of the distribution: ";
    std::cin >> a_dist;
    std::cout << "Enter an integer value for the upper bound of the distribution: ";
    std::cin >> b_dist;
    std::cout << "Enter an integer value for the sample count: ";
    std::cin >> samples;

    test(a_dist, b_dist, samples);
}
Use CTRL-Z to bypass data entry and run using default values.
Enter an integer value for the lower bound of the distribution: 0
Enter an integer value for the upper bound of the distribution: 12
Enter an integer value for the sample count: 200
lower bound == 0
upper bound == 12
Distribution for 200 samples:
    0 :::::::::::::::
    1 :::::::::::::::::::::
    2 ::::::::::::::::::
    3 :::::::::::::::
    4 :::::::
    5 :::::::::::::::::::::
    6 :::::::::::::
    7 ::::::::::
    8 :::::::::::::::
    9 :::::::::::::
   10 ::::::::::::::::::::::
   11 :::::::::::::
   12 :::::::::::::::::

需求

標頭: < random>

命名空間:std

uniform_int_distribution::uniform_int_distribution

建構分佈。

explicit uniform_int_distribution(
   result_type a = 0, result_type b = std::numeric_limits<result_type>::max());
explicit uniform_int_distribution(const param_type& parm);

參數

a
隨機值的下限 (內含)。

b
隨機值的上限 (內含)。

parm
用來建構分佈的 param_type 結構。

備註

前置條件︰a ≤ b

第一個建構函式會建構一個 物件,其儲存 的值會保留 值 a ,而儲存的 b 值則保留值 b

第二個建構函式會建構預存參數是從 parm 初始化而來的物件。 您可以呼叫 param() 成員函式,取得及設定現有分佈的目前參數。

uniform_int_distribution::param_type

儲存分佈的參數。

struct param_type {
   typedef uniform_int_distribution<result_type> distribution_type;
   param_type(
      result_type a = 0, result_type b = std::numeric_limits<result_type>::max());
   result_type a() const;
   result_type b() const;

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

參數

a
隨機值的下限 (內含)。

b
隨機值的上限 (內含)。

right
要與這個項目比較的 param_type 物件。

備註

前置條件︰a ≤ b

此結構可在具現化時傳遞至分佈的類別建構函式,傳遞至 param() 成員函式可設定現有分佈之儲存的參數,傳遞至 operator() 可用於取代儲存的參數。

另請參閱

<random>