C++11 random class uniform_real_distribution


C++11 random class uniform_real_distribution

The C++11 random uniform_real_distribution produce real number from the specified range.To specify the range the minimum and the maximum values should be passed as the arguments during the constructor call.The declaration of the uniform_real_distribution class is shown below.

template
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

typedef RealType result_type;
typedef unspecified param_type;

The param_type is usually a structure that stores information about the distribution fro instance the minimum and maximum of the distribution,but 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 during it’s object’s creation.Consider the code below.

Code example

uniform_real_distribution<double >urd(1.7 , 1.902) ; //set range from 1.7 to 1.902,so the distribution will generate real number from the range [1.7 , 1.902]

uniform_real_distribution<float >::param_type pt( 2.3 , 2.564 ) ; //param_type object declaration

uniform_real_distribution<float > urdf( pt ); //set teh 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.


 


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

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)

Link: C++11 linear_congruential_engine

Code example

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,

1.72677   1.8297   1.85424   1.83168

You can see that the numbers generated ar 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.

Code example

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,

2.33499   2.46951   2.50159   2.47209


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.

Code example

uniform_real_distribution<double> ur(9.01 , 9.404);

cout<< ur.a()

Output,

9.01

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.

Code example

uniform_real_distribution<double> ur(9.01 , 9.404);

cout<< ur.b()

Output

9.404

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.

Code example

uniform_real_distribution<double > ur(9.01 , 9.404) ;

uniform_real_distribution< >::param_type p ;
p=ur.param( );

cout<< p.a() << ”   ” << p.b() ;

Output

9.01   9.404

param(param_type)

Call this function if you want to change the range of the distribution.

Code example

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,

900.023   900.0345
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.

Code example

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.5   4.5
4.67   4.67

Do not get confused 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.