C++11 random random_device

The C++11 random 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.

class random_device ;

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

Types and characteristic

typedef unsigned int result_type ;
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.

Code example

random_device rd;

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

Output in Code::Blocks,




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

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.

Code example

random_number rd ;

cout<< rd() ;

Output in code::blocks,


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).

Link : C++ cstdlib log function

Code example

random_device rd;

cout<< rd.entropy( ) ;

Output in code::blocks,


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.

Link: C++11 =delete function

Code example

random_device rd , rd1 ;

rd=rd1; //error


Stop wasting time,earn money($$$) from your website-Join Now!