C++11 student_t distribution random number generator

The C++11 student_t distribution (or student_t_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 student_t distribution

The distribution class declaration is shown below.

template<class RealType = double>
class student_t_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 student_t_distribution of the probability distribution.

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


Types

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 student_t_distribution(RealType n = 1); 

explicit student_t_distribution(const param_type& parm); 

void reset( );

The first constructor accepts a parameter ‘n’ whose default value is 1.The default value will be same in all the compiler.The use of the parameter is to evaluate the probability of the random values in the distribution.The relation 0 < n on ‘n’ 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.

student_t_distribution< >sd ;

student_t_distribution<float > sd1;

student_t_distribution< float >::param_type pt(5.01 ) ;

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

reset()

The reset( ) function reset the distribution state.


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.

student_t_distribution< >sd ;

default_random_engine dre ;

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

Output in Code::blocks,

-2.45917   -0.115325

the second operator( ) function

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

student_t_distribution<float >sd ;

student_t_distribution< float >::param_type pt(5.01) ;

linear_congruential_engine<unsigned int , 193703 , 0 , 83474882 > lce ; //an engine

cout<< sd(lce , pt) << ” ” << sd(lce , pt) << endl ;

Link :C++11 Linear congruential engine



Property functions

RealType n() const;

param_type param() const; 

void param(const param_type& parm);

result_type min() const; 

result_type max() const; 
n() function

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

student_t_distribution< >sd ,

sd1( 900 , 10);

cout<< sd.n() << endl
<< sd1.n() ;

Output,

1
900

param()

This function returns the param_type object.

student_t_distribution< >sd( 123 );

cout<< sd.param().n() ;

Output,

123

param(param_type)

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

student_t_distribution<float > sd(5000 );

cout<< sd.n() << endl ;

student_t_distribution< float >::param_type pt( 56.01) ;

sd.param( pt );

cout<< sd.n() ;

Output,

5000
56.01

min() function

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

student_t_distribution<float > sd(5000 , 100);

cout<< sd.min( );

Output,

0

max() function

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

student_t_distribution<float > sd(5000 , 100);

cout<< sd.max( );

Output,

3.40282e+038


Side note

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

student_t__distribution probability function
 




Leave a Reply

Your email address will not be published. Required fields are marked *