random_device.hpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /* boost random/random_device.hpp header file
  2. *
  3. * Copyright Jens Maurer 2000
  4. * Copyright Steven Watanabe 2010-2011
  5. * Distributed under the Boost Software License, Version 1.0. (See
  6. * accompanying file LICENSE_1_0.txt or copy at
  7. * http://www.boost.org/LICENSE_1_0.txt)
  8. *
  9. * $Id$
  10. *
  11. * Revision history
  12. * 2000-02-18 Portability fixes (thanks to Beman Dawes)
  13. */
  14. // See http://www.boost.org/libs/random for documentation.
  15. #ifndef BOOST_RANDOM_RANDOM_DEVICE_HPP
  16. #define BOOST_RANDOM_RANDOM_DEVICE_HPP
  17. #include <string>
  18. #include <boost/config.hpp>
  19. #include <boost/noncopyable.hpp>
  20. #include <boost/random/detail/auto_link.hpp>
  21. #include <boost/system/config.hpp> // force autolink to find Boost.System
  22. namespace boost {
  23. namespace random {
  24. /**
  25. * Class \random_device models a \nondeterministic_random_number_generator.
  26. * It uses one or more implementation-defined stochastic processes to
  27. * generate a sequence of uniformly distributed non-deterministic random
  28. * numbers. For those environments where a non-deterministic random number
  29. * generator is not available, class random_device must not be implemented. See
  30. *
  31. * @blockquote
  32. * "Randomness Recommendations for Security", D. Eastlake, S. Crocker,
  33. * J. Schiller, Network Working Group, RFC 1750, December 1994
  34. * @endblockquote
  35. *
  36. * for further discussions.
  37. *
  38. * @xmlnote
  39. * Some operating systems abstract the computer hardware enough
  40. * to make it difficult to non-intrusively monitor stochastic processes.
  41. * However, several do provide a special device for exactly this purpose.
  42. * It seems to be impossible to emulate the functionality using Standard
  43. * C++ only, so users should be aware that this class may not be available
  44. * on all platforms.
  45. * @endxmlnote
  46. *
  47. * <b>Implementation Note for Linux</b>
  48. *
  49. * On the Linux operating system, token is interpreted as a filesystem
  50. * path. It is assumed that this path denotes an operating system
  51. * pseudo-device which generates a stream of non-deterministic random
  52. * numbers. The pseudo-device should never signal an error or end-of-file.
  53. * Otherwise, @c std::ios_base::failure is thrown. By default,
  54. * \random_device uses the /dev/urandom pseudo-device to retrieve
  55. * the random numbers. Another option would be to specify the /dev/random
  56. * pseudo-device, which blocks on reads if the entropy pool has no more
  57. * random bits available.
  58. *
  59. * <b>Implementation Note for Windows</b>
  60. *
  61. * On the Windows operating system, token is interpreted as the name
  62. * of a cryptographic service provider. By default \random_device uses
  63. * MS_DEF_PROV.
  64. *
  65. * <b>Performance</b>
  66. *
  67. * The test program <a href="\boost/libs/random/performance/nondet_random_speed.cpp">
  68. * nondet_random_speed.cpp</a> measures the execution times of the
  69. * random_device.hpp implementation of the above algorithms in a tight
  70. * loop. The performance has been evaluated on an
  71. * Intel(R) Core(TM) i7 CPU Q 840 \@ 1.87GHz, 1867 Mhz with
  72. * Visual C++ 2010, Microsoft Windows 7 Professional and with gcc 4.4.5,
  73. * Ubuntu Linux 2.6.35-25-generic.
  74. *
  75. * <table cols="2">
  76. * <tr><th>Platform</th><th>time per invocation [microseconds]</th></tr>
  77. * <tr><td> Windows </td><td>2.9</td></tr>
  78. * <tr><td> Linux </td><td>1.7</td></tr>
  79. * </table>
  80. *
  81. * The measurement error is estimated at +/- 1 usec.
  82. */
  83. class random_device : private noncopyable
  84. {
  85. public:
  86. typedef unsigned int result_type;
  87. BOOST_STATIC_CONSTANT(bool, has_fixed_range = false);
  88. /** Returns the smallest value that the \random_device can produce. */
  89. static result_type min BOOST_PREVENT_MACRO_SUBSTITUTION () { return 0; }
  90. /** Returns the largest value that the \random_device can produce. */
  91. static result_type max BOOST_PREVENT_MACRO_SUBSTITUTION () { return ~0u; }
  92. /** Constructs a @c random_device, optionally using the default device. */
  93. BOOST_RANDOM_DECL random_device();
  94. /**
  95. * Constructs a @c random_device, optionally using the given token as an
  96. * access specification (for example, a URL) to some implementation-defined
  97. * service for monitoring a stochastic process.
  98. */
  99. BOOST_RANDOM_DECL explicit random_device(const std::string& token);
  100. BOOST_RANDOM_DECL ~random_device();
  101. /**
  102. * Returns: An entropy estimate for the random numbers returned by
  103. * operator(), in the range min() to log2( max()+1). A deterministic
  104. * random number generator (e.g. a pseudo-random number engine)
  105. * has entropy 0.
  106. *
  107. * Throws: Nothing.
  108. */
  109. BOOST_RANDOM_DECL double entropy() const;
  110. /** Returns a random value in the range [min, max]. */
  111. BOOST_RANDOM_DECL unsigned int operator()();
  112. /** Fills a range with random 32-bit values. */
  113. template<class Iter>
  114. void generate(Iter begin, Iter end)
  115. {
  116. for(; begin != end; ++begin) {
  117. *begin = (*this)();
  118. }
  119. }
  120. private:
  121. class impl;
  122. impl * pimpl;
  123. };
  124. } // namespace random
  125. using random::random_device;
  126. } // namespace boost
  127. #endif /* BOOST_RANDOM_RANDOM_DEVICE_HPP */