C++11 cauchy distribution random number generator

The C++11 cauchy distribution (or cauchy_distribution) produces random numbers ‘x’ using the respective discrete probability function of the distribution-the function is shown at the end of the post.

Link :C++11 random number generator
C++11 cauchy distribution random number generator

The distribution class declaration is shown below.

template<class RealType = double>
class cauchy_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 cauchy distribution of the probability 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 cauchy_distribution(RealType a = 0.0 , RealType b = 1.0);

explicit cauchy_distribution(const param_type& parm); 

void reset( );

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

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

cauchy_distribution< >cd ;

cauchy_distribution<float > cd1;

cauchy_distribution< float >::param_type pt( 20 , 4.56 ) ;

cauchy_distribution< long double >: cd2(pt) ; //error! , type of pt is float but type of cd2 is double type


The reset( ) function reset the distribution.Note this function does nothing in cauchy_distribution.

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.

cauchy_distribution< >cd ;

default_random_engine dre ;

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

Output in Code::blocks,

7.65462   0.438486

the second operator( ) function

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

cauchy_distribution<float >cd ;

cauchy_distribution< float >::param_type pt(20 , 4.56 ) ;

knuth_b kb ; //an engine

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

Output in Code::blocks,

31.88  84 21.0353

Property functions

result_type a( ) const ;

result_type b() const;

param_type param() const; 

void param(const param_type& parm); 

result_type min() const; 

result_type max() const; 
a() function

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

cauchy_distribution< >cd ,

evd1( 9 , 10);

cout<< cd.a() << endl
<< cd1.a() ;



b() function

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

cauchy_distribution< >cd ,

evd1( 5000 , 10);

cout<< cd.b() << endl
<< cd1.b() ; 




This function returns the param_type object.

cauchy_distribution< >cd( 123 , 893);

cout<< cd.param().a() << endl
<< cd.param().b() ;




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

cauchy_distribution<float > cd(5000 , 100);

cout<< cd.a() << endl ;

cauchy_distribution< float >::param_type pt( 56.01 , 6.7 ) ;

cd.param( pt );

cout<< cd.a() ;



min() function

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

cauchy_distribution<float > cd(5000 , 100);

cout<< cd.min( ); 



max() function

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

cauchy_distribution<float > cd(5000 , 100);
cout<< cd.max( );



Side note

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

C++11 cauchy distribution probability function