discrete_distribution Class
Generates a discrete integer distribution that has uniform-width intervals with uniform probability in each interval.
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;
};
Parameters
- IntType
The integer result type, defaults to int. For possible types, see <random>.
Remarks
This sampling distribution has uniform-width intervals with uniform probability in each interval. For information about other sampling distributions, see piecewise_linear_distribution Class and piecewise_constant_distribution Class.
The following table links to articles about individual members:
discrete_distribution::param |
|
discrete_distribution::operator() |
The property function vector<double> probabilities() returns the individual probabilities for each integer generated.
For more information about distribution classes and their members, see <random>.
Example
// 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);
}
Output
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 ::::::::::::::::::::::::::::::::::
Requirements
Header: <random>
Namespace: std