C++11 random exponential_distribution

The C++11 random exponential_distribution produces random numbers x < 0.The random sequence generated is of floating point type,we cannot obtain integer random sequence using this distribution.The declaration of the class is shown below.

template<class RealType = double>
class exponential_distribution;

The default template type of the distribution is double type.,you can however change the type to other type by passing that type during the distribution class object declaration.

All the types and member functions of the class is shown below.


typedef RealType result_type;
typedef unspecified param_type;

The param_type is a structure .It’s type definition is shown as unspecified as the definition of the param_type is compiler dependent.

Constructors and reset function

explicit exponential_distribution(RealType lambda = 1.0);
explicit exponential_distribution(const param_type& parm);
void reset();

The first constructor accept a parameter known as ‘lambda’ ,this parameter is used in calculating the probability of the random values in the distribution.And the ‘lambda’ must be always greater than 0.

The second constructor accept a reference to param_type object.Here the ‘lambda’ value of the distribution is initialized with the ‘lambda’ value of the param_type object-param_type constructor also accept a parameter name ‘lambda’.

Code example

exponential_distribution< > ed(9.0); //lambda value is 9.0

exponential_distribution<float >::param_type ed( 8.8 );

exponential_distribution<float > ed1( pt) ; //mean value is 88 taken from the ‘mean’ value of the pt object

exponential_distribution< > ed2( pt) ; //error! , type of pt is float but type of ed2 is double type

reset( ) function

This function resets the distribution.It does nothing in exponential_distribution.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 generated random sequence is obtained using the operator() function.The first overloaded operator() accept URNG(Uniform Random Number Generator) or engine.

Code example

exponential_distribution< > ed(9.0);

default_random_engine dre;

cout<< ed(dre) << ” ” << ed(dre) ;

Output in Code::blocks,

0.0681877   0.0156701

This output will change from compiler to compiler as the default_random_engine is implementation-defined.

Link : C++11 default_random_engine

the second operator() function

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

Code example

exponential_distribution< > ed ;

exponential_distribution< >::param_type pt ;

linear_congruential_engine<unsigned int , 193703 , 0 , 83474882 > lce ;

cout<< ed(lce ) << ” ” << ed(lce) ;

Output in Code::blocks,

1.02748 0.142169

Property functions

RealType lambda() const;
param_type param() const;
void param(const param_type& parm);
result_type min() const;
result_type max() const;

lambda() function

This function returns the ‘lambda’ value of the distribution.

Code example

exponential_distribution< > ed(9.0);

cout<< ed.lambda() ;

Output ,



This function returns the param_type object.

Code example

exponential_distribution<float >::param_type pt( 8.8 );

cout<< pt.lambda( ) << endl ; //access the ‘lambda’ value

exponential_distribution<float > ed(3.4);

pt=ed.param( );

cout<< pt.lambda( ) ;



since pt was assigned with the returned value of param() function the ‘lambda’ value was also changed.


Using this function we can change the ‘lambda’ value of the distribution to the lambda value of the param_type object by passing the param_type object.

Code example

exponential_distribution<long double >ed(8.9) ;

cout<< ed.lambda( ) << endl ;

exponential_distribution<float >::param_type ed( 10.2 );


cout<< ed.lambda( ) ;



min() function

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

Code example

exponential_distribution< > ed(100) ;

cout<< pd.min( ) ;



max() function

The max() returns the largest value the distribution can generate.It returns the value of numeric_limits<result_type>::max().

Code example

exponential_distribution< > ed ;

cout<< pd.max( ) ;



The numeric_limits<double>::max() also gives the value 1.79769e+308.

*Side Note

exponential_distribution roduces random numbers x>0 distributed according to the probability density function,

  p(x|λ) =λe−λx

The λ signify the ‘lambda’ value.


Stop wasting time,earn money($$$) from your website-Join Now!