Boost C++ Libraries Home Libraries People FAQ More

PrevUpHomeNext

Class random_device

boost::random::random_device

Synopsis

// In header: <boost/random/random_device.hpp>


class random_device : private  {
public:
  // types
  typedef unsigned int ;

  // construct/copy/destruct
  ();
  (const );
  ~();

  // public member functions
   (bool,  = false);
  BOOST_RANDOM_DECL double () ;
  BOOST_RANDOM_DECL unsigned int ();
  template<typename Iter> void (Iter, Iter);

  // public static functions
  BOOST_CONSTEXPR result_type  ();
  BOOST_CONSTEXPR result_type  ();
};

Description

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.

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

  1. ();

    Constructs a random_device, optionally using the default device.

  2. (const  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.

  3. ~();

random_device public member functions

  1.  (bool,  = false);
  2. BOOST_RANDOM_DECL double () ;

    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.

  3. BOOST_RANDOM_DECL unsigned int ();

    Returns a random value in the range [min, max].

  4. template<typename Iter> void (Iter begin, Iter end);

    Fills a range with random 32-bit values.

random_device public static functions

  1. BOOST_CONSTEXPR result_type  ();

    Returns the smallest value that the random_device can produce.

  2. BOOST_CONSTEXPR result_type  ();

    Returns the largest value that the random_device can produce.


PrevUpHomeNext