C++11 uniform int distribution ( or uniform_int_distribution ) random

The C++11 uniform int distribution (or uniform_int_distribution) produces random integers from the specified range.The limiting range value is usually passed as an arguments to the constructor during the object declaration.

C++110 uniform int distribution
Link : C++11 random number generator

The declaration of the uniform_int_distribution class is shown below.

template<class IntType = int>
class uniform_int_distribution; 

During the class declaration it is require to pass a template argument type and which of course should be an integer type since the distribution support only integer type.The default type is ‘int’ type.All the types and member functions of the class is given below.


typedef IntType result_type; 

typedef <em>unspecified</em> param_type;

The definition of the param_type will differ from compiler to compiler.One thing however which might be common about param_type in all the compiler is that it is a structure that stores the information of the minimum and maximum value of the range within which the distribution can generate a random value.You will see later that by calling param_typedirectly we can set the range without calling the uniform_int_distribution class constructor.

Constructors and reset function

C++11 uniform_int_distribution has two constructors.

explicit uniform_int_distribution(IntType a = 0 , IntType b = numeric_limits&lt;IntType&gt;::max() ); </th>explicit 

uniform_int_distribution(const param_type& parm);

void reset(); 

If no arguments is passed during the uniform_int_distribution object declaration the minimum value is taken as 0 and the maximum value is given by numeric_limits<IntType>(IntType is the type of the class).

The second constructor accept a param_type,in this case the minimum and maximum value of the distribution is same as the values passed to the param_type during it’s constructor call;as already mentioned param_type is a structure.

uniform_int_distribution<signed int> uid(0 , 100); 
//set the range from 0 to 100,so the distribution can generate values only from within the range [0 , 100]

uniform_int_distribution< >::param_type pt( 500 , 700); 
//calls ‘param_type’ and sets the range from 500 to 700,the type is ‘int’ type since it is the default type

In the declaration of the ‘param_type’ if no arguments-to specify the range- are pass the range is defaulted to [0 , numeric_limits<IntType>].How to verify the limiting range value is shown later.

reset() function

The ‘reset()’ function reset the state of the distribution but note in uniform_int_distribution this function does nothing.You can neglect it.

Generating functions

Generating functions are those fucntioncs that will generate 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

The generating function i.e. operator() function, helps you to obtain the random number from the distribution.Since the distribution requires an engine we will pass the engine as an argument to the operator() function.A code example is given below.

uniform_int_distribution<signed int> uid(0 , 100);

default_random_engine dre ;

//output four integers in the range [0 , 100] 
for(auto i=0 ; i<4 ; i++)
cout<< uid( dre ) << ” “;

Output in Code::Blocks,

0   13   76   46

The second operator() function

The second ‘operator()’ function accepts URNG(Uniform Random Number Generator) and a pram_type object.This operator() overloaded version is used only when you have specified the range by calling the param_type object.Consider the code below.

uniform_int_distribution<int> ud ; //no range specified

uniform_int_distribution< >::param_type pt( 500 , 700); //range specified here

default_random_engine dre ;

//output four integers in the range [500 , 700]
for(auto i=0 ; i<4 ; i++)
cout<< ud( dre , pt ) << ” “;

Output in Code::blocks,

500 668 500 555

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_int_distribution<long long> uid(905 , 1000);
out<< uid.a()



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_int_distribution<long long> uid(905 , 1000);

cout<< uid.b()



Note param_type also has it’s own ‘b()’ function which returns the maximum value of the distribution.


The param() functions returns the pram_type object.

uniform_int_distribution<long int > ui(90 , 120) , ;

uniform_int_distribution<long int>::param_type p ;
p=ui.param( );

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


90   120


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

uniform_int_distribution< long > uid(90 , 900 );
cout<< uid.a() << ”   ” << uid.b() << endl ;

uniform_int_distribution<long int>::param_type pa( 150 , 700 );

uid.param( pa );

cout<< uid.a() << ”   ” << uid.b() << endl ;


90   900
150   700

Calling param(pa) change the range of the distribution to [150 , 700].

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_int_distribution< long > uid(90 , 900 );

cout<< uid.min.() << ”   ” << uid.param().a() << endl ;

cout<< uid.max() << ”   ” << uid.param.b() << endl ;


90   90
900   900

Do not get confused with ‘uid.param().a()’ or ‘uid.param().b()’,calling param() returns the param_type object ,using that object we call the a() and b() function.

Side note

uniform_int_distribution produces random integers ‘i‘,a≤ i ≤ b ,according to the constant discrete probability function,

P(i | a,b) = 1 /(b−a + 1)