nondet_random.qbk 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. [/
  2. / Copyright (c) 2009 Steven Watanabe
  3. /
  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. [section Header <boost/nondet_random.hpp> Synopsis]
  9. namespace boost {
  10. class random_device;
  11. } // namespace boost
  12. [endsect]
  13. [section Class random_device]
  14. [section Synopsis]
  15. class random_device : noncopyable
  16. {
  17. public:
  18. typedef unsigned int result_type;
  19. static const bool has_fixed_range = true;
  20. static const result_type min_value = /* implementation defined */;
  21. static const result_type max_value = /* implementation defined */;
  22. result_type min() const;
  23. result_type max() const;
  24. explicit random_device(const std::string& token = default_token);
  25. ~random_device();
  26. double entropy() const;
  27. unsigned int operator()();
  28. };
  29. [endsect]
  30. [section Description]
  31. Class `random_device` models a non-deterministic random number generator. It
  32. uses one or more implementation-defined stochastic processes to generate a
  33. sequence of uniformly distributed non-deterministic random numbers. For those
  34. environments where a non-deterministic random number generator is not
  35. available, class random_device must not be implemented. See
  36. [:"Randomness Recommendations for Security", D. Eastlake, S. Crocker,
  37. J. Schiller, Network Working Group, RFC 1750, December 1994]
  38. for further discussions.
  39. [note Some operating systems abstract the computer hardware enough to make it
  40. difficult to non-intrusively monitor stochastic processes. However, several do
  41. provide a special device for exactly this purpose. It seems to be impossible
  42. to emulate the functionality using Standard C++ only, so users should be aware
  43. that this class may not be available on all platforms.]
  44. [endsect]
  45. [section Members]
  46. explicit random_device(const std::string& token = default_token)
  47. Effects: Constructs a random_device, optionally using the given token as an
  48. access specification (for example, a URL) to some implementation-defined
  49. service for monitoring a stochastic process.
  50. double entropy() const
  51. Returns: An entropy estimate for the random numbers returned by `operator()`,
  52. in the range `min()` to `log2(max()+1)`. A deterministic random number
  53. generator (e.g. a pseudo-random number engine) has entropy 0.
  54. Throws: Nothing.
  55. [endsect]
  56. Implementation Note for Linux
  57. On the Linux operating system, token is interpreted as a filesystem path. It
  58. is assumed that this path denotes an operating system pseudo-device which
  59. generates a stream of non-deterministic random numbers. The pseudo-device
  60. should never signal an error or end-of-file. Otherwise, std::ios_base::failure
  61. is thrown. By default, random_device uses the /dev/urandom pseudo-device to
  62. retrieve the random numbers. Another option would be to specify the
  63. /dev/random pseudo-device, which blocks on reads if the entropy pool has no
  64. more random bits available.
  65. [endsect]
  66. [section Performance]
  67. The test program nondet_random_speed.cpp measures the execution times of the
  68. nondet_random.hpp implementation of the above algorithms in a tight loop.
  69. The performance has been evaluated on a Pentium Pro 200 MHz with gcc 2.95.2,
  70. Linux 2.2.13, glibc 2.1.2.
  71. [table preformance
  72. [[class] [time per invocation \[usec\]]]
  73. [[random_device] [92.0]]
  74. ]
  75. The measurement error is estimated at +/- 1 usec.
  76. [endsect]