|
||||
File indexing completed on 2025-01-18 09:51:12
0001 /* boost random/random_device.hpp header file 0002 * 0003 * Copyright Jens Maurer 2000 0004 * Copyright Steven Watanabe 2010-2011 0005 * Distributed under the Boost Software License, Version 1.0. (See 0006 * accompanying file LICENSE_1_0.txt or copy at 0007 * http://www.boost.org/LICENSE_1_0.txt) 0008 * 0009 * $Id$ 0010 * 0011 * Revision history 0012 * 2000-02-18 Portability fixes (thanks to Beman Dawes) 0013 */ 0014 0015 // See http://www.boost.org/libs/random for documentation. 0016 0017 0018 #ifndef BOOST_RANDOM_RANDOM_DEVICE_HPP 0019 #define BOOST_RANDOM_RANDOM_DEVICE_HPP 0020 0021 #include <string> 0022 #include <boost/config.hpp> 0023 #include <boost/noncopyable.hpp> 0024 #include <boost/random/detail/auto_link.hpp> 0025 #include <boost/system/config.hpp> // force autolink to find Boost.System 0026 0027 namespace boost { 0028 namespace random { 0029 0030 /** 0031 * Class \random_device models a \nondeterministic_random_number_generator. 0032 * It uses one or more implementation-defined stochastic processes to 0033 * generate a sequence of uniformly distributed non-deterministic random 0034 * numbers. For those environments where a non-deterministic random number 0035 * generator is not available, class random_device must not be implemented. See 0036 * 0037 * @blockquote 0038 * "Randomness Recommendations for Security", D. Eastlake, S. Crocker, 0039 * J. Schiller, Network Working Group, RFC 1750, December 1994 0040 * @endblockquote 0041 * 0042 * for further discussions. 0043 * 0044 * @xmlnote 0045 * Some operating systems abstract the computer hardware enough 0046 * to make it difficult to non-intrusively monitor stochastic processes. 0047 * However, several do provide a special device for exactly this purpose. 0048 * It seems to be impossible to emulate the functionality using Standard 0049 * C++ only, so users should be aware that this class may not be available 0050 * on all platforms. 0051 * @endxmlnote 0052 * 0053 * <b>Implementation Note for Linux</b> 0054 * 0055 * On the Linux operating system, token is interpreted as a filesystem 0056 * path. It is assumed that this path denotes an operating system 0057 * pseudo-device which generates a stream of non-deterministic random 0058 * numbers. The pseudo-device should never signal an error or end-of-file. 0059 * Otherwise, @c std::ios_base::failure is thrown. By default, 0060 * \random_device uses the /dev/urandom pseudo-device to retrieve 0061 * the random numbers. Another option would be to specify the /dev/random 0062 * pseudo-device, which blocks on reads if the entropy pool has no more 0063 * random bits available. 0064 * 0065 * <b>Implementation Note for Windows</b> 0066 * 0067 * On the Windows operating system, token is interpreted as the name 0068 * of a cryptographic service provider. By default \random_device uses 0069 * MS_DEF_PROV. 0070 * 0071 * <b>Performance</b> 0072 * 0073 * The test program <a href="\boost/libs/random/performance/nondet_random_speed.cpp"> 0074 * nondet_random_speed.cpp</a> measures the execution times of the 0075 * random_device.hpp implementation of the above algorithms in a tight 0076 * loop. The performance has been evaluated on an 0077 * Intel(R) Core(TM) i7 CPU Q 840 \@ 1.87GHz, 1867 Mhz with 0078 * Visual C++ 2010, Microsoft Windows 7 Professional and with gcc 4.4.5, 0079 * Ubuntu Linux 2.6.35-25-generic. 0080 * 0081 * <table cols="2"> 0082 * <tr><th>Platform</th><th>time per invocation [microseconds]</th></tr> 0083 * <tr><td> Windows </td><td>2.9</td></tr> 0084 * <tr><td> Linux </td><td>1.7</td></tr> 0085 * </table> 0086 * 0087 * The measurement error is estimated at +/- 1 usec. 0088 */ 0089 class random_device : private noncopyable 0090 { 0091 public: 0092 typedef unsigned int result_type; 0093 BOOST_STATIC_CONSTANT(bool, has_fixed_range = false); 0094 0095 /** Returns the smallest value that the \random_device can produce. */ 0096 static BOOST_CONSTEXPR result_type min BOOST_PREVENT_MACRO_SUBSTITUTION () { return 0; } 0097 /** Returns the largest value that the \random_device can produce. */ 0098 static BOOST_CONSTEXPR result_type max BOOST_PREVENT_MACRO_SUBSTITUTION () { return ~0u; } 0099 0100 /** Constructs a @c random_device, optionally using the default device. */ 0101 BOOST_RANDOM_DECL random_device(); 0102 /** 0103 * Constructs a @c random_device, optionally using the given token as an 0104 * access specification (for example, a URL) to some implementation-defined 0105 * service for monitoring a stochastic process. 0106 */ 0107 BOOST_RANDOM_DECL explicit random_device(const std::string& token); 0108 0109 BOOST_RANDOM_DECL ~random_device(); 0110 0111 /** 0112 * Returns: An entropy estimate for the random numbers returned by 0113 * operator(), in the range min() to log2( max()+1). A deterministic 0114 * random number generator (e.g. a pseudo-random number engine) 0115 * has entropy 0. 0116 * 0117 * Throws: Nothing. 0118 */ 0119 BOOST_RANDOM_DECL double entropy() const; 0120 /** Returns a random value in the range [min, max]. */ 0121 BOOST_RANDOM_DECL unsigned int operator()(); 0122 0123 /** Fills a range with random 32-bit values. */ 0124 template<class Iter> 0125 void generate(Iter begin, Iter end) 0126 { 0127 for(; begin != end; ++begin) { 0128 *begin = (*this)(); 0129 } 0130 } 0131 0132 private: 0133 class impl; 0134 impl * pimpl; 0135 }; 0136 0137 } // namespace random 0138 0139 using random::random_device; 0140 0141 } // namespace boost 0142 0143 #endif /* BOOST_RANDOM_RANDOM_DEVICE_HPP */
[ Source navigation ] | [ Diff markup ] | [ Identifier search ] | [ general search ] |
This page was automatically generated by the 2.3.7 LXR engine. The LXR team |