Chris@16: /* boost random/random_device.hpp header file Chris@16: * Chris@16: * Copyright Jens Maurer 2000 Chris@16: * Copyright Steven Watanabe 2010-2011 Chris@16: * Distributed under the Boost Software License, Version 1.0. (See Chris@16: * accompanying file LICENSE_1_0.txt or copy at Chris@16: * http://www.boost.org/LICENSE_1_0.txt) Chris@16: * Chris@101: * $Id$ Chris@16: * Chris@16: * Revision history Chris@16: * 2000-02-18 Portability fixes (thanks to Beman Dawes) Chris@16: */ Chris@16: Chris@16: // See http://www.boost.org/libs/random for documentation. Chris@16: Chris@16: Chris@16: #ifndef BOOST_RANDOM_RANDOM_DEVICE_HPP Chris@16: #define BOOST_RANDOM_RANDOM_DEVICE_HPP Chris@16: Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@101: #include // force autolink to find Boost.System Chris@16: Chris@16: namespace boost { Chris@16: namespace random { Chris@16: Chris@16: /** Chris@16: * Class \random_device models a \nondeterministic_random_number_generator. Chris@16: * It uses one or more implementation-defined stochastic processes to Chris@16: * generate a sequence of uniformly distributed non-deterministic random Chris@16: * numbers. For those environments where a non-deterministic random number Chris@16: * generator is not available, class random_device must not be implemented. See Chris@16: * Chris@16: * @blockquote Chris@16: * "Randomness Recommendations for Security", D. Eastlake, S. Crocker, Chris@16: * J. Schiller, Network Working Group, RFC 1750, December 1994 Chris@16: * @endblockquote Chris@16: * Chris@16: * for further discussions. Chris@16: * Chris@16: * @xmlnote Chris@16: * Some operating systems abstract the computer hardware enough Chris@16: * to make it difficult to non-intrusively monitor stochastic processes. Chris@16: * However, several do provide a special device for exactly this purpose. Chris@16: * It seems to be impossible to emulate the functionality using Standard Chris@16: * C++ only, so users should be aware that this class may not be available Chris@16: * on all platforms. Chris@16: * @endxmlnote Chris@16: * Chris@16: * Implementation Note for Linux Chris@16: * Chris@16: * On the Linux operating system, token is interpreted as a filesystem Chris@16: * path. It is assumed that this path denotes an operating system Chris@16: * pseudo-device which generates a stream of non-deterministic random Chris@16: * numbers. The pseudo-device should never signal an error or end-of-file. Chris@16: * Otherwise, @c std::ios_base::failure is thrown. By default, Chris@16: * \random_device uses the /dev/urandom pseudo-device to retrieve Chris@16: * the random numbers. Another option would be to specify the /dev/random Chris@16: * pseudo-device, which blocks on reads if the entropy pool has no more Chris@16: * random bits available. Chris@16: * Chris@16: * Implementation Note for Windows Chris@16: * Chris@16: * On the Windows operating system, token is interpreted as the name Chris@16: * of a cryptographic service provider. By default \random_device uses Chris@16: * MS_DEF_PROV. Chris@16: * Chris@16: * Performance Chris@16: * Chris@16: * The test program Chris@16: * nondet_random_speed.cpp measures the execution times of the Chris@16: * random_device.hpp implementation of the above algorithms in a tight Chris@16: * loop. The performance has been evaluated on an Chris@16: * Intel(R) Core(TM) i7 CPU Q 840 \@ 1.87GHz, 1867 Mhz with Chris@16: * Visual C++ 2010, Microsoft Windows 7 Professional and with gcc 4.4.5, Chris@16: * Ubuntu Linux 2.6.35-25-generic. Chris@16: * Chris@16: * Chris@16: * Chris@16: * Chris@16: * Chris@16: *
Platformtime per invocation [microseconds]
Windows 2.9
Linux 1.7
Chris@16: * Chris@16: * The measurement error is estimated at +/- 1 usec. Chris@16: */ Chris@16: class random_device : private noncopyable Chris@16: { Chris@16: public: Chris@16: typedef unsigned int result_type; Chris@16: BOOST_STATIC_CONSTANT(bool, has_fixed_range = false); Chris@16: Chris@16: /** Returns the smallest value that the \random_device can produce. */ Chris@16: static result_type min BOOST_PREVENT_MACRO_SUBSTITUTION () { return 0; } Chris@16: /** Returns the largest value that the \random_device can produce. */ Chris@16: static result_type max BOOST_PREVENT_MACRO_SUBSTITUTION () { return ~0u; } Chris@16: Chris@16: /** Constructs a @c random_device, optionally using the default device. */ Chris@16: BOOST_RANDOM_DECL random_device(); Chris@16: /** Chris@16: * Constructs a @c random_device, optionally using the given token as an Chris@16: * access specification (for example, a URL) to some implementation-defined Chris@16: * service for monitoring a stochastic process. Chris@16: */ Chris@16: BOOST_RANDOM_DECL explicit random_device(const std::string& token); Chris@16: Chris@16: BOOST_RANDOM_DECL ~random_device(); Chris@16: Chris@16: /** Chris@16: * Returns: An entropy estimate for the random numbers returned by Chris@16: * operator(), in the range min() to log2( max()+1). A deterministic Chris@16: * random number generator (e.g. a pseudo-random number engine) Chris@16: * has entropy 0. Chris@16: * Chris@16: * Throws: Nothing. Chris@16: */ Chris@16: BOOST_RANDOM_DECL double entropy() const; Chris@16: /** Returns a random value in the range [min, max]. */ Chris@16: BOOST_RANDOM_DECL unsigned int operator()(); Chris@16: Chris@16: /** Fills a range with random 32-bit values. */ Chris@16: template Chris@16: void generate(Iter begin, Iter end) Chris@16: { Chris@16: for(; begin != end; ++begin) { Chris@16: *begin = (*this)(); Chris@16: } Chris@16: } Chris@16: Chris@16: private: Chris@16: class impl; Chris@16: impl * pimpl; Chris@16: }; Chris@16: Chris@16: } // namespace random Chris@16: Chris@16: using random::random_device; Chris@16: Chris@16: } // namespace boost Chris@16: Chris@16: #endif /* BOOST_RANDOM_RANDOM_DEVICE_HPP */