Boost C++ Libraries of the most highly regarded and expertly designed C++ library projects in the world. Herb Sutter and Andrei Alexandrescu, C++ Coding Standards

This is the documentation for an old version of boost. Click here for the latest Boost documentation.

Class random_device



// In header: <boost/nondet_random.hpp>

class random_device {
  // types
  typedef unsigned int result_type;

  // construct/copy/destruct
  random_device(const std::string & = default_token);

  // 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] 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.


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 construct/copy/destruct

  1. random_device(const std::string & token = default_token);

    Constructs a random_device, optionally using the given token as an access specification (for example, a URL) to some implementation-defined service for monitoring a stochastic process.

  2. ~random_device();

random_device public member functions

  1. result_type min() const;

    Returns: The smallest value that the random_device can produce.

  2. result_type max() const;

    Returns: The largest value that the random_device can produce.

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

  4. BOOST_RANDOM_DECL unsigned int operator()();

    Returns: A random value in the range [min, max]