C++11 piecewise constant distribution random number generator

The C++11 piecewise constant distribution (or piecewise_constant_distribution) produces floating point random values ‘x’ using a specific probability density function-the function is provided at the end of this post.(Better check out the link below if you do not know what is distribution)

Link :C++ random number generator
C++11 piecewise constant distribution

The piecewise_constant_distribution template class declaration is shown below.

template<class RealType = double>
class piecewise_constant_distribution;

The default type of the distribution is ‘double’ type ,so the default constructed object always produce double type random numbers.

All the distribution’s types and member function is given below.


typedef IntType result_type;

typedef unspecified param_type;

The param_type is as structure whose definition is compiler dependent,that is also why it’s type is mentioned as unspecified.

Constructors and reset function


template<class InputIteratorB, class InputIteratorW>
piecewise_constant_distribution(InputIteratorB firstB, InputIteratorB lastB,
InputIteratorW firstW);

template<class UnaryOperation>
piecewise_constant_distribution(initializer_list&lt;RealType&gt; bl, UnaryOperation fw); 

template<class UnaryOperation>
piecewise_constant_distribution(size_t nw, RealType xmin, RealType xmax, UnaryOperation fw);

explicit piecewise_constant_distribution(const param_type& parm);

void reset( );

You can see that the piecewise_constant_distribution have five constructors:
a)The first constructor is the default constructor.
b)The second constructor accept three arguments: firstB and lastB iterator pointing to the first and one past the last element of the same container and the firstW iterator pointing to another different container.The container can be vector or an array or any equivalent type.And note the third iterator must point to container having at least the same number of elements as the container pointed by the first iterator.
c)The third constructor accept initializer_list and a function.The initializer_list type must be real type and the unary function must return a double type or any type convertible to double type.
d)The fourth constructor accept 4 arguments.Refer to the first three arguments type from the above declaration and the fourth argument is a function which return a double type or a type convertible to double type.
e)The fifth constructor accept param_type object.

The program below shows how to call all the constructors.

double func(double arg)
return arg;

int main( )
piecewise_constant_distribution< > pcd; //calls the default constructor

vector<int> vec={ 20 , 30 , 4 , 67 } , vec1={ 2 ,3 ,4 ,9};

piecewise_constant_distribution< > pcd1(vec.begin() , vec.end() , vec1.begin() ); //calls the second constructor

initializer_list< double > il={ 2.3 , 6.7 , 9.034 };
piecewise_constant_distribution< > pcd2( il , func ); //calls the third constructor

piecewise_constant_distribution<float > pcd3( 9 , 0 , 100 , func ); //calls the 4th constructor

piecewise_constant_distribution<float >::param_type pt;

piecewise_constant_distribution<float > pcd4(pt) ; //calls the 5th constructor

piecewise_constant_distribution<long double > pc5(pt) ; //error! pt is ‘float’ type but pcd5 is ‘long double’ type

return 0;


The reset() function reset the state of the distribution.It does nothing in discrete_distribution so you can neglect it.

Generating functions

template<class URNG>
result_type operator( )(URNG& g); 

template<class URNG>
result_type operator( )(URNG& g, const param_type& parm); 
the first operator() function

The random sequence is obtained using the operator() function.The first overloaded operator() accept URNG(Uniform Random Number Generator) or engine.

piecewise_constant_distribution< > pcd;

default_random_engine dre;

cout<< pcd(dre) << ” ” << pcd(dre) ;

Output in Code::blocks,

0.45865   0.131538

the second operator( ) function

The second overloaded operator( ) function accept URNG and param_type object.

piecewise_constant_distribution<float >::param_type pt( 9 , 0 , 100 , func ); //same as the 4th constructor

piecewise_constant_distribution<float > pcd;

knuth_b kb ; //a predefined engine type

cout<< pcd(dre , pt) << ” ” << pcd(dre , pt) ;

Output in code::blocks,

97.2346   61.6734

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;
vector<result_type> intervals( ) const;

This function returns vector of ‘result_type’.The content of the returned vector will depend on which constructor is called during the object creation:
a)If default constructor is called the return vector consists of only two elements 0 and 1.
b)If the second constructor is called the return vector content is same as the content of the container whose iterators pointing to the first and one past the last element is passed as the first and second argument.Simply if ‘vec.begin()’ and ‘vec.end()’ -vec is any vector object- are passed as the first and second iterator then ‘vec’ content is returned.
c)If the third constructor is called.The returned vector will have the element as the initializer_list content.
d)If the fourth constructor is called then the content is
bk=xmin+k · δ , k=0 , 1, 2 … n,
0 < δ= (xmax − xmin ) /n, xmax is the third argument and xmin is the fourth argument and ‘n’ is the first argument passed to the constructor.

e)If the fifth constructor is called,the param_type object may call any of the four constructors describe above then the content of the returned vector will hold true as described for each of the constructor called.

piecewise_constant_distribution< > pcd ;

vector<double> vec={2 , 4 };

vec=pcd.intervals( );

cout<< vec[0] << ” ” vec[1] << “\n\n” ;

vector< double > vec1={ 2 , 34 , 45 , 10 } , vec3={ 0 , 0 , 0 , 0 };

vector< double > vecRet{ 0 , 0 , 0 , 1};

piecewise_constant_distribution< >::param_type pt( vec1.begin( ) , vec1.end( ) , vec3.begin( ) );

piecewise_constant_distribution< > pcd1( pt ) ;

vecRet=pcd1.intervals( );

for(auto elem:vecRet )
cout<< elem << ” ” ;

Output in Code::Blocks,

0   1

2   34   45   10

The ‘pt’ accepted ‘vec.begin()’ and ‘vec.end()’ as the first and second iterator so the returned vector vecRet content is same as the content of ‘vec’.

densities( ) const; function

This function returns a vector of double type.The returned vector stores the probability densities of the random number generated by the distribution.

piecewise_constant_distribution< > pcd ;

vector<double> vec={ 0 , 23};

vec=pcd.densities( );

cout<< vec[0] ;

Output in Visual Studio,


param( )

This function returns the param_type object.

piecewise_constant_distribution< >pcd ;

piecewise_constant_distribution< >::param_type pt;

pt=pcd.param() ;

Using this function we can change the parameter of the distribution to the parameter of the param_type object by passing the param_type object.

initializer_list< double > il ={ 2 , 3} ,

il1={ 90 , 100 };

vector<double > vec1={0 , 0} ,
  vec2={ 0 , 0}

piecewise_constant_distribution< > pcd(il) ;


cout<< vec1[0] << ” ” << vec1[1] << endl;

piecewise_constant_distribution< >::param_type pt(il1 , func) ; //func is same as func from the constructor section code.

pcd.param( pt );

vec2=pcd.intervals( );

cout<< vec1[0] << ” ” << vec1[1] ;

Output ,

2   3
90   100

Calling param(pt) changes the parameter of the pcd to pt parameter values.

min() function

The min() returns the smallest value the distribution can generate,which is the value 0.

piecewise_constant_distribution< > pcd;

cout<< pcd.min() ;
max() function

The max() returns the largest value the distribution can generate.

 piecewise_constant_distribution< > pcd( 9 , 0 , 100 , func );
cout<< pcd.max() ;

Output ,