nondet_random.hpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /* boost nondet_random.hpp header file
  2. *
  3. * Copyright Jens Maurer 2000
  4. * Distributed under the Boost Software License, Version 1.0. (See
  5. * accompanying file LICENSE_1_0.txt or copy at
  6. * http://www.boost.org/LICENSE_1_0.txt)
  7. *
  8. * $Id: nondet_random.hpp 62347 2010-05-31 16:44:36Z steven_watanabe $
  9. *
  10. * Revision history
  11. * 2000-02-18 Portability fixes (thanks to Beman Dawes)
  12. */
  13. // See http://www.boost.org/libs/random for documentation.
  14. #ifndef BOOST_NONDET_RANDOM_HPP
  15. #define BOOST_NONDET_RANDOM_HPP
  16. #include <string> // std::abs
  17. #include <algorithm> // std::min
  18. #include <boost/config/no_tr1/cmath.hpp>
  19. #include <boost/config.hpp>
  20. #include <boost/utility.hpp> // noncopyable
  21. #include <boost/integer_traits.hpp> // compile-time integral limits
  22. #include <boost/random/detail/auto_link.hpp>
  23. namespace boost {
  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. * nondet_random.hpp implementation of the above algorithms in a tight
  70. * loop. The performance has been evaluated on a Pentium Pro 200 MHz
  71. * with gcc 2.95.2, Linux 2.2.13, glibc 2.1.2.
  72. *
  73. * <table cols="2">
  74. * <tr><th>class</th><th>time per invocation [usec]</th></tr>
  75. * <tr><td> @xmlonly <classname alt="boost::random_device">random_device</classname> @endxmlonly </td><td>92.0</td></tr>
  76. * </table>
  77. *
  78. * The measurement error is estimated at +/- 1 usec.
  79. */
  80. class random_device : private noncopyable
  81. {
  82. public:
  83. typedef unsigned int result_type;
  84. BOOST_STATIC_CONSTANT(bool, has_fixed_range = true);
  85. BOOST_STATIC_CONSTANT(result_type, min_value = integer_traits<result_type>::const_min);
  86. BOOST_STATIC_CONSTANT(result_type, max_value = integer_traits<result_type>::const_max);
  87. /**
  88. * Returns: The smallest value that the \random_device can produce.
  89. */
  90. result_type min BOOST_PREVENT_MACRO_SUBSTITUTION () const { return min_value; }
  91. /**
  92. * Returns: The largest value that the \random_device can produce.
  93. */
  94. result_type max BOOST_PREVENT_MACRO_SUBSTITUTION () const { return max_value; }
  95. /**
  96. * Constructs a @c random_device, optionally using the given token as an
  97. * access specification (for example, a URL) to some implementation-defined
  98. * service for monitoring a stochastic process.
  99. */
  100. BOOST_RANDOM_DECL explicit random_device(const std::string& token = default_token);
  101. BOOST_RANDOM_DECL ~random_device();
  102. /**
  103. * Returns: An entropy estimate for the random numbers returned by
  104. * operator(), in the range min() to log2( max()+1). A deterministic
  105. * random number generator (e.g. a pseudo-random number engine)
  106. * has entropy 0.
  107. *
  108. * Throws: Nothing.
  109. */
  110. BOOST_RANDOM_DECL double entropy() const;
  111. /**
  112. * Returns: A random value in the range [min, max]
  113. */
  114. BOOST_RANDOM_DECL unsigned int operator()();
  115. private:
  116. BOOST_RANDOM_DECL static const char * const default_token;
  117. /*
  118. * std:5.3.5/5 [expr.delete]: "If the object being deleted has incomplete
  119. * class type at the point of deletion and the complete class has a
  120. * non-trivial destructor [...], the behavior is undefined".
  121. * This disallows the use of scoped_ptr<> with pimpl-like classes
  122. * having a non-trivial destructor.
  123. */
  124. class impl;
  125. impl * pimpl;
  126. };
  127. // TODO: put Schneier's Yarrow-160 algorithm here.
  128. } // namespace boost
  129. #endif /* BOOST_NONDET_RANDOM_HPP */