variate_generator.hpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /* boost random/variate_generator.hpp header file
  2. *
  3. * Copyright Jens Maurer 2002
  4. * Copyright Steven Watanabe 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. * See http://www.boost.org for most recent version including documentation.
  10. *
  11. * $Id$
  12. *
  13. */
  14. #ifndef BOOST_RANDOM_RANDOM_GENERATOR_HPP
  15. #define BOOST_RANDOM_RANDOM_GENERATOR_HPP
  16. #include <boost/random/detail/ptr_helper.hpp>
  17. #include <boost/random/detail/disable_warnings.hpp>
  18. namespace boost {
  19. /// \cond hide_private_members
  20. namespace random {
  21. ///\endcond
  22. /**
  23. * A random variate generator is used to join a random number
  24. * generator together with a random number distribution.
  25. * Boost.Random provides a vast choice of \generators as well
  26. * as \distributions.
  27. *
  28. * The argument for the template parameter Engine shall be of
  29. * the form U, U&, or U*, where U models a
  30. * \uniform_random_number_generator. Then, the member
  31. * engine_value_type names U (not the pointer or reference to U).
  32. *
  33. * Specializations of @c variate_generator satisfy the
  34. * requirements of CopyConstructible. They also satisfy the
  35. * requirements of Assignable unless the template parameter
  36. * Engine is of the form U&.
  37. *
  38. * The complexity of all functions specified in this section
  39. * is constant. No function described in this section except
  40. * the constructor throws an exception.
  41. */
  42. template<class Engine, class Distribution>
  43. class variate_generator
  44. {
  45. private:
  46. typedef boost::random::detail::ptr_helper<Engine> helper_type;
  47. public:
  48. typedef typename helper_type::value_type engine_value_type;
  49. typedef Engine engine_type;
  50. typedef Distribution distribution_type;
  51. typedef typename Distribution::result_type result_type;
  52. /**
  53. * Constructs a @c variate_generator object with the associated
  54. * \uniform_random_number_generator eng and the associated
  55. * \random_distribution d.
  56. *
  57. * Throws: If and what the copy constructor of Engine or
  58. * Distribution throws.
  59. */
  60. variate_generator(Engine e, Distribution d)
  61. : _eng(e), _dist(d) { }
  62. /** Returns: distribution()(engine()) */
  63. result_type operator()() { return _dist(engine()); }
  64. /**
  65. * Returns: distribution()(engine(), value).
  66. */
  67. template<class T>
  68. result_type operator()(const T& value) { return _dist(engine(), value); }
  69. /**
  70. * Returns: A reference to the associated uniform random number generator.
  71. */
  72. engine_value_type& engine() { return helper_type::ref(_eng); }
  73. /**
  74. * Returns: A reference to the associated uniform random number generator.
  75. */
  76. const engine_value_type& engine() const { return helper_type::ref(_eng); }
  77. /** Returns: A reference to the associated \random_distribution. */
  78. distribution_type& distribution() { return _dist; }
  79. /**
  80. * Returns: A reference to the associated random distribution.
  81. */
  82. const distribution_type& distribution() const { return _dist; }
  83. /**
  84. * Precondition: distribution().min() is well-formed
  85. *
  86. * Returns: distribution().min()
  87. */
  88. result_type min BOOST_PREVENT_MACRO_SUBSTITUTION () const { return (distribution().min)(); }
  89. /**
  90. * Precondition: distribution().max() is well-formed
  91. *
  92. * Returns: distribution().max()
  93. */
  94. result_type max BOOST_PREVENT_MACRO_SUBSTITUTION () const { return (distribution().max)(); }
  95. private:
  96. Engine _eng;
  97. distribution_type _dist;
  98. };
  99. } // namespace random
  100. using random::variate_generator;
  101. } // namespace boost
  102. #include <boost/random/detail/enable_warnings.hpp>
  103. #endif // BOOST_RANDOM_RANDOM_GENERATOR_HPP