C++11 uniform real distribution class random number generator
The C++11 uniform real distribution(or uniform_real_distribution) class produce real number within the specified range.To specify the range the minimum and the maximum values should be passed as the arguments during the constructor call.
Link : C++11 random number generator
The declaration of the uniform_real_distribution class is shown below.
template<class RealType = double> class uniform_real_distribution;
The default type of the class is the ‘double’ type,however you can change it by passing the type as template argument during the class object’s declaration.
The types and member functions of uniform_real_distribution class is given below.
Types
C++11 random uniform_real_distribution has two data types.
typedef RealType result_type; typedef unspecified param_type;
The param_type is usually a structure that stores information about the distribution,for instance the minimum and maximum of the distribution,how this structure is defined varies from compiler to compiler.How to use this type will become clearer later so read on.
Constructors and reset function
explicit uniform_real_distribution(RealType a = 0.0, RealType b = 1.0); explicit uniform_real_distribution(const param_type& parm); void reset();
During the declaration of the object if no range is specified (no arguments are passed to the constructor) the range is defaulted to [0 , 1] as given by the parameters ‘a’ and ‘b’.
On the other hand if param_type is passed to the distribution’s constructor the range is taken from the arguments passed to the param_type.Consider the code below.
uniform_real_distribution<double >urd(1.7 , 1.902) ; //set range from 1.7 to 1.902,generating real number from the range [1.7 , 1.902] //Below param_type object declaration example uniform_real_distribution<float >::param_type pt( 2.3 , 2.564 ) ; uniform_real_distribution<float > urdf( pt ); //set the distribution range to [2.3 , 2.564]
Note during the param_type object’s creation if no range is specified the defaulted range is [0 , 1].Also note the type of ‘pt‘ and ‘urdf‘ must match,in our case it matches to float type,if it doesn’t the compiler will complain.
reset() function
The ‘reset()’ function reset the state of the distribution but note in uniform_real_distribution this function does nothing.You can neglect it.
Generating functions
In C++11 uniform_real_distribution by generating function we mean the function that generates the random sequence.
template<class URNG> result_type operator( )(URNG& g); template<class URNG> result_type operator( )(URNG& g, const param_type& parm);
the first operator() function
Using the operator() function we can get the random numbers but the engine object must be passed as an argument.(URNG stands for Uniform Random Number Generator)
uniform_real_distribution<double >urd(1.7 , 1.902) ; linear_congruential_engine<unsigned int , 193703 , 0 , 83474882 >lce ; //output four real numbers for( auto i=0; i<4 ; i++) { cout<< urd( lce ) << ” ” ; }
Output in code::blocks,
You can see that the numbers generated are all within the range [1.7 , 1.902].
The second operator() function
The second overloaded operator() accepts URNG and a reference to param_type object.Call this version only when you have not specified the range during the distribution object creation and you want to use the range specified by the param_type object.Consider the code below.
uniform_real_distribution<double> urd1 ; linear_congruential_engine<unsigned int , 193703 , 0 , 83474882 >lce ; uniform_real_distribution<float >::param_type pt( 2.3 , 2.564 ) ; //output four real numbers for( auto i=0; i<4 ; i++) { cout<< urd1( lce , pt) << ” ” ; }
Output in code::blocks,
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 least value the distribution can generate.
uniform_real_distribution<double> ur(9.01 , 9.404); cout<< ur.a()
Output,
Note param_type also has ‘a()’ function which returns the minimum value of the distribution .
b() function
This function returns the maximum value the distribution can represent.
uniform_real_distribution<double> ur(9.01 , 9.404); cout<< ur.b()
Output
Note param_type also has it’s own ‘b()’ function which returns the maximum value of the distribution.
param()
The param() functions returns the pram_type object.
uniform_real_distribution<double > ur(9.01 , 9.404) ; uniform_real_distribution< >::param_type p ; p=ur.param( ); cout<< p.a() << ” ” << p.b() ;
Output
param(param_type)
Call this function if you want to change the range of the distribution.
uniform_real_distribution< long double> ur(900.023 , 900.0345 ); cout<< ur.a() << ” ” << ur.b() << endl ; uniform_real_distribution<long double>::param_type pa( 34.52 , 35 ); ur.param( pa ); cout<< uid.a() << ” ” << uid.b() << endl ;
Output,
34.52 35
Calling ‘param(pa)’ change the range of the distribution to [34.52 , 35].
min() function
This function is same as the a() function.It gives the smallest value of the distribution.
max() function
This function is same as the b() function.It gives the maximum value of the distribution.
uniform_real_distribution<float > urd(4.5 , 4.67 ); cout<< urd.min.() << ” ” << urd.param().a() << endl ; cout<< urd.max() << ” ” << urd.param.b() << endl ;
Output ,
4.67 4.67
Do not get confuse with ‘urd.param().a()’ or ‘urd.param().b()’,calling param() returns the param_type object ,using that object we call the a() and b() function.