Home | Libraries | People | FAQ | More |
boost::random::random_device
// In header: <boost/random/random_device.hpp> class random_device { public: // types typedef unsigned int result_type; // construct/copy/destruct random_device(); explicit random_device(const std::string &); ~random_device(); // public static functions static result_type min(); static result_type max(); // public member functions double entropy() const; unsigned int operator()(); template<typename Iter> void generate(Iter, Iter); // public data members static const bool has_fixed_range; };
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 random_device.hpp implementation of the above algorithms in a tight loop. The performance has been evaluated on an Intel(R) Core(TM) i7 CPU Q 840 @ 1.87GHz, 1867 Mhz with Visual C++ 2010, Microsoft Windows 7 Professional and with gcc 4.4.5, Ubuntu Linux 2.6.35-25-generic.
Platform | time per invocation [microseconds] |
Windows | 2.9 |
Linux | 1.7 |
The measurement error is estimated at +/- 1 usec.
random_device
public
construct/copy/destructrandom_device();
Constructs a
, optionally using the default device. random_device
explicit random_device(const std::string & token);
Constructs a
, optionally using the given token as an access specification (for example, a URL) to some implementation-defined service for monitoring a stochastic process. random_device
~random_device();
random_device
public static functionsstatic result_type min();
Returns the smallest value that the random_device
can produce.
static result_type max();
Returns the largest value that the random_device
can produce.
random_device
public member functionsdouble 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.
unsigned int operator()();
Returns a random value in the range [min, max].
template<typename Iter> void generate(Iter begin, Iter end);
Fills a range with random 32-bit values.