piecewise_constant_distribution Class

Generates a piecewise constant distribution that has varying-width intervals with uniform probability in each interval.

template<class RealType = double>
class piecewise_constant_distribution
{
public:
    // types
    typedef RealType result_type;
    struct param_type;
    // constructor and reset functions
    piecewise_constant_distribution();
    template<class InputIteratorI, class InputIteratorW>
    piecewise_constant_distribution(InputIteratorI firstI, InputIteratorI lastI, InputIteratorW firstW);
    template<class UnaryOperation>
    piecewise_constant_distribution(initializer_list<RealType> intervals, UnaryOperation weightfunc);
    template<class UnaryOperation>
    piecewise_constant_distribution(size_t count, RealType xmin, RealType xmax, UnaryOperation weightfunc);
    explicit piecewise_constant_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<result_type> intervals() const;
    vector<result_type> densities() const;
    param_type param() const;
    void param(const param_type& parm);
    result_type min() const;
    result_type max() const;
};

Parameters

  • RealType
    The floating point result type, defaults to double. For possible types, see <random>.

Remarks

This sampling distribution has varying-width intervals with uniform probability in each interval. For information about other sampling distributions, see piecewise_linear_distribution Class and discrete_distribution.

The following table links to articles about individual members:

piecewise_constant_distribution::piecewise_constant_distribution

piecewise_constant_distribution::intervals

piecewise_constant_distribution::param

piecewise_constant_distribution::operator()

piecewise_constant_distribution::densities

piecewise_constant_distribution::param_type

The property function intervals() returns a vector<RealType> with the set of stored intervals of the distribution.

The property function densities() returns a vector<RealType> with the stored densities for each interval set, which are calculated according to the weights provided in the constructor parameters.

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);

    // Three intervals, non-uniform: 0 to 1, 1 to 6, and 6 to 15
    vector<double> intervals{ 0, 1, 6, 15 };
    // weights determine the densities used by the distribution
    vector<double> weights{ 1, 5, 10 };

    piecewise_constant_distribution<double> distr(intervals.begin(), intervals.end(), weights.begin());

    cout << endl;
    cout << "min() == " << distr.min() << endl;
    cout << "max() == " << distr.max() << endl;
    cout << "intervals (index: interval):" << endl;
    vector<double> i = distr.intervals();
    int counter = 0;
    for (const auto& n : i) {
        cout << fixed << setw(11) << counter << ": " << setw(14) << setprecision(10) << n << endl;
        ++counter;
    }
    cout << endl;
    cout << "densities (index: density):" << endl;
    vector<double> d = distr.densities();
    counter = 0;
    for (const auto& n : d) {
        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 << '-' << elem.first+1 << ' ' << 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() == 15
intervals (index: interval):
          0:   0.0000000000
          1:   1.0000000000
          2:   6.0000000000
          3:  15.0000000000

densities (index: density):
          0:   0.0625000000
          1:   0.0625000000
          2:   0.0694444444

Distribution for 100 samples:
    0-1 :::::::
    1-2 ::::::
    2-3 :::::
    3-4 ::::::
    4-5 :::::::
    5-6 ::::::
    6-7 :::
    7-8 ::::::::::
    8-9 ::::::
    9-10 ::::::::::::
   10-11 :::::
   11-12 ::::::
   12-13 :::::::::
   13-14 ::::
   14-15 ::::::::

Requirements

Header: <random>

Namespace: std

See Also

Reference

<random>

piecewise_linear_distribution