C++11 random lognormal_distribution

C++11 random lognormal_distribution

The C++11 random lognormal_distribution produces random numbers x>0 using the respective discrete probability function of the distribution-the function is shown at the end of the post.The distribution class declaration is shown below.

template<class RealType = double>
class lognormal_distribution;

The class default type is double type and note this distribution can generate only floating point type values or real numbers.

The distribution is based on the the lognormal distribution of the probability distribution.

Link :https://en.wikipedia.org/wiki/Log-normal_distribution

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


typedef RealType result_type;
typedef unspecified param_type;

The RealType is a type definition of the template type and the param_type is a structure but note the definition of the param_type will alter from compiler to compiler.

Constructors and reset function

explicit lognormal_distribution(RealType m = 0.0, RealType s = 1.0);
explicit lognormal_distribution(const param_type& parm);
void reset( );

The first constructor accepts two parameters ‘m’ and ‘s’ whose default values are 0 and 1.These default values will be same in all compiler.The uses of these two parameters is to evaluate the probability of the random values in the distribution.The relation 0 < s on ‘s’ should hold.

The second constructor accept param_type object and in this case the values of ‘m’ and ‘s’ is deduced from the ‘m’ and ‘s’ values of the param_type object.

Code example

lognormal_distribution< > lnd ;

lognormal_distribution<float > lnd1;

lognormal_distribution< float >::param_type pt(23 , 607 ) ;

lognormal_distribution< long double > lnd2(pt) ; //error! , type of pt is float but type of lnd2 is double type


The reset( ) function reset the distribution state.


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

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

lognormal_distribution< >lnd ;

default_random_engine dre ;

cout<< lnd(dre) << ” ” << lnd(dre) << endl ;

Output in Code::blocks,

-1.08682   -0.121966

the second operator( ) function

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

Link : C++11 random knuth_b

Code example

lognormal_distribution< >lnd ;

lognormal_distribution< float >::param_type pt(23 , 607 ) ;

knuth_b kb ; //an engine

cout<< lnd(kb , pt) << ” ” << lnd(kb , pt) << endl ;

Output in Code::blocks,

-378.848   -86.2217

Property functions

result_type m( ) const ;
result_type s() const;
param_type param() const;
void param(const param_type& parm);
result_type min() const;
result_type max() const;

m() function

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

Code example

lognormal_distribution< > lnd ,
lnd1( 900 , 10);

cout<< lnd.m() << endl
<< lnd1.m() ;



s() function

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

Code example

lognormal_distribution< > lnd ,
lnd1( 900 , 10);

cout<< lnd.s() << endl
<< lnd1.s() ;




This function returns the param_type object.

Code example

lognormal_distribution< >lnd( 123 , 893);

cout<< lnd.param().m() << endl
<< lnd.param().s() ;




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

Code example

lognormal_distribution<float > lnd(5000 , 100);

cout<< lnd.m() << endl ;

lognormal_distribution< float >::param_type pt( 23 , 607 ) ;

evd.param( pt );

cout<< lnd.m() ;



min() function

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

Code example

lognormal_distribution<float > evd(5000 , 100);

cout<< evd.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

lognormal_distribution<float > evd(5000 , 100);

cout<< evd.max( );



*Side Note

lognormal_distribution produces random numbers x distributed according to the probability density function,

extreme_value_distribtion probability function

Link :C++ cmath exp function