public member function
<random>
(1) | discrete_distribution(); |
---|
(2) | template <class InputIterator> discrete_distribution (InputIterator first, InputIterator last); |
---|
(3) | discrete_distribution (initializer_list<double> il); |
---|
(4) | template <class UnaryOperation> discrete_distribution (size_t nw, double xmin, double xmax, UnaryOperation fw); |
---|
(5) | explicit discrete_distribution (const param_type& parm); |
---|
Construct discrete distribution
Constructs a discrete_distribution object, initializing it depending on the constructor version used:
- (1) default constructor
- The distribution will always produce zero.
- (2) range constructor
- The sequence of values in the range is used as the weights for each integer value from 0 to (n-1), where n is the distance between the iterators.
- (3) initializer list
- The sequence in the list is used as the weights for each integer value from 0 to (n-1), where n is the size of the initializer list.
- (4) operation constructor
- Each integer value k in the closed range between 0 and (nw-1) is given the weight resulting from calling:
1
|
fw(xmin+(xmax-xmin)*(k+0.5)/nw)
|
- (5) param constructor
- The distribution adopts the distribution parameters specified by parm.
In all the cases above, if n (or nw) is zero, the distribution is constructed as if a single element with a weight of one (w0=1) was defined. Such a distribution will always produce zero.
All weights shall be non-negative values, and at least one of the values in the sequence must be positive.
Parameters
- first, last
- Input iterators to the initial and final positions in a range. The range used is [first,last), which includes all the elements between first and last, including the element pointed by first but not the element pointed by last.
If first==last, the distribution will always produce zero.
The function template type shall be an input iterator that points to some type convertible to double.
- il
- An initializer_list object.
These objects are automatically constructed from initializer list declarators.
- nw
- Number of weights: The highest possible number produced by the distribution is (nw-1).
- xmin,xmax
- Bounds of a range of values to be passed to fw. The first value passed to fw is (xmin+(xmax-xmin)/2*n), and then, nw values are passed with increases of ((xmax-xmin)/2).
- fw
- A pointer to a function or a function object that can take a single argument of a type convertible from double and return a value convertible to double.
- parm
- An object representing the distribution's parameters, obtained by a call to member function param.
param_type is a member type.
Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
|
// discrete_distribution constructor
#include <iostream>
#include <chrono>
#include <random>
#include <functional>
#include <array>
int main()
{
// construct a trivial random generator engine from a time-based seed:
unsigned seed = std::chrono::system_clock::now().time_since_epoch().count();
std::default_random_engine generator (seed);
std::array<double,4> init = {1.0,2.0,3.0,4.0};
std::discrete_distribution<int> first;
std::discrete_distribution<int> second (init.begin(),init.end());
std::discrete_distribution<int> third {0.1,0.2,0.3,0.4};
std::discrete_distribution<int> fourth (4,0.0,40.0,std::bind2nd(std::plus<double>(),5.0));
std::discrete_distribution<int> fifth (fourth.param());
// display probabilities:
std::cout << "displaying probabilities:";
std::cout << std::endl << "first : ";
for (double x:first.probabilities()) std::cout << x << " ";
std::cout << std::endl << "second: ";
for (double x:second.probabilities()) std::cout << x << " ";
std::cout << std::endl << "third : ";
for (double x:third.probabilities()) std::cout << x << " ";
std::cout << std::endl << "fourth: ";
for (double x:fourth.probabilities()) std::cout << x << " ";
std::cout << std::endl << "fifth : ";
for (double x:fifth.probabilities()) std::cout << x << " ";
std::cout << std::endl;
return 0;
}
|
Output:
displaying probabilities:
first : 1
second: 0.1 0.2 0.3 0.4
third : 0.1 0.2 0.3 0.4
fourth: 0.1 0.2 0.3 0.4
fifth : 0.1 0.2 0.3 0.4
|
Complexity
Linear in the number of possible values, at most.