C++11 random_device random number generator

The C++11 random_device is a random number generator which produces non-deterministic random numbers.This generator may be useful if the implementation prevent generation of non-deterministic random numbers.The declaration of the class is shown below.

Link : C++11 random number generator

class random_device ;

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


Types and characteristic

typedef unsigned int result_type ;</th></tr>

static constexpr result_type min() { return numeric_limits<result_type>::min(); } 

static constexpr result_type max() { return numeric_limits<result_type>::max(); } 

The min() and max gives the smallest and largest number the class can generate.

random_device rd;

cout<< rd.min() << endl
<< rd.max() << endl ;

Output in Code::Blocks,

0
4294967295



Constructors

explicit random_device(const string& token = implementation-defined ); 

The constructor construct a non-deterministic uniform random number generator object.The default value of the ‘token’ is also implementation defined.It throws a value derived from the ‘exception’ which is implementation defined.


Member functions

C++11 random_device has four member functions.

result_type operator()(); 

double entropy() const noexcept; 

random_device(const random_device& ) = delete; 

void operator=(const random_device& ) = delete; 

operator() function

To get the random number we use the ‘operator()’ function.It throws a value derived from the ‘exception’ which is implementation defined if the random number could not be obtained.

random_number rd ;

cout<< rd() ; 

Output in code::blocks,

3499211612

 
entropy( ) function

It returns 0 if the implementation employs random number engine.Otherwise it returns an entropy estimate for the random numbers returned by operator( ),in the range min() to log2( max( ) + 1).

random_device rd;

cout<< rd.entropy( ) ;

Output in code::blocks,

0

Visual Studio gives 32,check the output in your compiler.


Copy constructor and operator=

Since the copy constructor and the operator assignment functions are defined as ‘=delete’,they cannot be called.

random_device rd , rd1 ;

rd=rd1; //error



Leave a Reply

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