...one of the most highly
regarded and expertly designed C++ library projects in the
world.
— Herb Sutter and Andrei
Alexandrescu, C++
Coding Standards
boost::random_device
// In header: <boost/nondet_random.hpp> class random_device { public: // types typedef unsigned int result_type; // construct/copy/destruct random_device(const std::string & = default_token); ~random_device(); // public member functions result_type min() const; result_type max() const; BOOST_RANDOM_DECL double entropy() const; BOOST_RANDOM_DECL unsigned int operator()(); static const bool has_fixed_range; static const result_type min_value; static const result_type max_value; };
Class random_device models a non-deterministic random number generator . It uses one or more implementation-defined stochastic processes to generate a sequence of uniformly distributed non-deterministic random numbers. For those environments where a non-deterministic random number generator is not available, class random_device must not be implemented. See
"Randomness Recommendations for Security", D. Eastlake, S. Crocker, J. Schiller, Network Working Group, RFC 1750, December 1994
for further discussions.
Note | |
---|---|
Some operating systems abstract the computer hardware enough to make it difficult to non-intrusively monitor stochastic processes. However, several do provide a special device for exactly this purpose. It seems to be impossible to emulate the functionality using Standard C++ only, so users should be aware that this class may not be available on all platforms. |
Implementation Note for Linux
On the Linux operating system, token is interpreted as a filesystem path. It is assumed that this path denotes an operating system pseudo-device which generates a stream of non-deterministic random numbers. The pseudo-device should never signal an error or end-of-file. Otherwise, std::ios_base::failure
is thrown. By default, random_device uses the /dev/urandom pseudo-device to retrieve the random numbers. Another option would be to specify the /dev/random pseudo-device, which blocks on reads if the entropy pool has no more random bits available.
Implementation Note for Windows
On the Windows operating system, token is interpreted as the name of a cryptographic service provider. By default random_device uses MS_DEF_PROV.
Performance
The test program nondet_random_speed.cpp measures the execution times of the nondet_random.hpp implementation of the above algorithms in a tight loop. The performance has been evaluated on a Pentium Pro 200 MHz with gcc 2.95.2, Linux 2.2.13, glibc 2.1.2.
class | time per invocation [usec] |
random_device | 92.0 |
The measurement error is estimated at +/- 1 usec.
random_device
public member functionsresult_type min() const;
Returns: The smallest value that the random_device
can produce.
result_type max() const;
Returns: The largest value that the random_device
can produce.
BOOST_RANDOM_DECL double entropy() const;
Returns: An entropy estimate for the random numbers returned by operator(), in the range min() to log2( max()+1). A deterministic random number generator (e.g. a pseudo-random number engine) has entropy 0.
Throws: Nothing.
BOOST_RANDOM_DECL unsigned int operator()();
Returns: A random value in the range [min, max]