Класс 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. Возможные типы см. в разделе <random>.
Заметки
Выборочное распределение использует равномерные интервалы ширины с одинаковой вероятностью в каждом интервале. Сведения о других выборочных распределениях см. в разделах Класс piecewise_linear_distribution и Класс piecewise_constant_distribution.
В следующей таблице представлены ссылки на статьи об отдельных членах.
discrete_distribution::param |
|
discrete_distribution::operator() |
Функция свойства vector<double> probabilities() возвращает отдельные вероятности для каждого полученного целого числа.
Дополнительные сведения о классах распределений и их членах см. в разделе <random>.
Пример
// 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);
}
Вывод
Требования
Заголовок: <random>
Пространство имен: std