uniform_01.hpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. /* boost random/uniform_01.hpp header file
  2. *
  3. * Copyright Jens Maurer 2000-2001
  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. * See http://www.boost.org for most recent version including documentation.
  9. *
  10. * $Id$
  11. *
  12. * Revision history
  13. * 2001-02-18 moved to individual header files
  14. */
  15. #ifndef BOOST_RANDOM_UNIFORM_01_HPP
  16. #define BOOST_RANDOM_UNIFORM_01_HPP
  17. #include <iostream>
  18. #include <boost/config.hpp>
  19. #include <boost/limits.hpp>
  20. #include <boost/static_assert.hpp>
  21. #include <boost/random/detail/config.hpp>
  22. #include <boost/random/detail/ptr_helper.hpp>
  23. #include <boost/random/detail/disable_warnings.hpp>
  24. namespace boost {
  25. namespace random {
  26. #ifdef BOOST_RANDOM_DOXYGEN
  27. /**
  28. * The distribution function uniform_01 models a \random_distribution.
  29. * On each invocation, it returns a random floating-point value
  30. * uniformly distributed in the range [0..1).
  31. *
  32. * The template parameter RealType shall denote a float-like value type
  33. * with support for binary operators +, -, and /.
  34. *
  35. * Note: The current implementation is buggy, because it may not fill
  36. * all of the mantissa with random bits. I'm unsure how to fill a
  37. * (to-be-invented) @c boost::bigfloat class with random bits efficiently.
  38. * It's probably time for a traits class.
  39. */
  40. template<class RealType = double>
  41. class uniform_01
  42. {
  43. public:
  44. typedef RealType input_type;
  45. typedef RealType result_type;
  46. result_type min BOOST_PREVENT_MACRO_SUBSTITUTION () const;
  47. result_type max BOOST_PREVENT_MACRO_SUBSTITUTION () const;
  48. void reset();
  49. template<class Engine>
  50. result_type operator()(Engine& eng);
  51. #ifndef BOOST_RANDOM_NO_STREAM_OPERATORS
  52. template<class CharT, class Traits>
  53. friend std::basic_ostream<CharT,Traits>&
  54. operator<<(std::basic_ostream<CharT,Traits>& os, const new_uniform_01&)
  55. {
  56. return os;
  57. }
  58. template<class CharT, class Traits>
  59. friend std::basic_istream<CharT,Traits>&
  60. operator>>(std::basic_istream<CharT,Traits>& is, new_uniform_01&)
  61. {
  62. return is;
  63. }
  64. #endif
  65. };
  66. #else
  67. namespace detail {
  68. template<class RealType>
  69. class new_uniform_01
  70. {
  71. public:
  72. typedef RealType input_type;
  73. typedef RealType result_type;
  74. // compiler-generated copy ctor and copy assignment are fine
  75. result_type min BOOST_PREVENT_MACRO_SUBSTITUTION () const { return result_type(0); }
  76. result_type max BOOST_PREVENT_MACRO_SUBSTITUTION () const { return result_type(1); }
  77. void reset() { }
  78. template<class Engine>
  79. result_type operator()(Engine& eng) {
  80. for (;;) {
  81. typedef typename Engine::result_type base_result;
  82. result_type factor = result_type(1) /
  83. (result_type(base_result((eng.max)()-(eng.min)())) +
  84. result_type(std::numeric_limits<base_result>::is_integer ? 1 : 0));
  85. result_type result = result_type(base_result(eng() - (eng.min)())) * factor;
  86. if (result < result_type(1))
  87. return result;
  88. }
  89. }
  90. #ifndef BOOST_RANDOM_NO_STREAM_OPERATORS
  91. template<class CharT, class Traits>
  92. friend std::basic_ostream<CharT,Traits>&
  93. operator<<(std::basic_ostream<CharT,Traits>& os, const new_uniform_01&)
  94. {
  95. return os;
  96. }
  97. template<class CharT, class Traits>
  98. friend std::basic_istream<CharT,Traits>&
  99. operator>>(std::basic_istream<CharT,Traits>& is, new_uniform_01&)
  100. {
  101. return is;
  102. }
  103. #endif
  104. };
  105. template<class UniformRandomNumberGenerator, class RealType>
  106. class backward_compatible_uniform_01
  107. {
  108. typedef boost::random::detail::ptr_helper<UniformRandomNumberGenerator> traits;
  109. public:
  110. typedef UniformRandomNumberGenerator base_type;
  111. typedef RealType result_type;
  112. BOOST_STATIC_CONSTANT(bool, has_fixed_range = false);
  113. #if !defined(BOOST_NO_LIMITS_COMPILE_TIME_CONSTANTS)
  114. BOOST_STATIC_ASSERT(!std::numeric_limits<RealType>::is_integer);
  115. #endif
  116. explicit backward_compatible_uniform_01(typename traits::rvalue_type rng)
  117. : _rng(rng),
  118. _factor(result_type(1) /
  119. (result_type((base().max)()-(base().min)()) +
  120. result_type(std::numeric_limits<base_result>::is_integer ? 1 : 0)))
  121. {
  122. }
  123. // compiler-generated copy ctor and copy assignment are fine
  124. result_type min BOOST_PREVENT_MACRO_SUBSTITUTION () const { return result_type(0); }
  125. result_type max BOOST_PREVENT_MACRO_SUBSTITUTION () const { return result_type(1); }
  126. typename traits::value_type& base() { return traits::ref(_rng); }
  127. const typename traits::value_type& base() const { return traits::ref(_rng); }
  128. void reset() { }
  129. result_type operator()() {
  130. for (;;) {
  131. result_type result = result_type(base()() - (base().min)()) * _factor;
  132. if (result < result_type(1))
  133. return result;
  134. }
  135. }
  136. #if !defined(BOOST_NO_OPERATORS_IN_NAMESPACE) && !defined(BOOST_NO_MEMBER_TEMPLATE_FRIENDS)
  137. template<class CharT, class Traits>
  138. friend std::basic_ostream<CharT,Traits>&
  139. operator<<(std::basic_ostream<CharT,Traits>& os, const backward_compatible_uniform_01& u)
  140. {
  141. os << u._rng;
  142. return os;
  143. }
  144. template<class CharT, class Traits>
  145. friend std::basic_istream<CharT,Traits>&
  146. operator>>(std::basic_istream<CharT,Traits>& is, backward_compatible_uniform_01& u)
  147. {
  148. is >> u._rng;
  149. return is;
  150. }
  151. #endif
  152. private:
  153. typedef typename traits::value_type::result_type base_result;
  154. UniformRandomNumberGenerator _rng;
  155. result_type _factor;
  156. };
  157. #ifndef BOOST_NO_INCLASS_MEMBER_INITIALIZATION
  158. // A definition is required even for integral static constants
  159. template<class UniformRandomNumberGenerator, class RealType>
  160. const bool backward_compatible_uniform_01<UniformRandomNumberGenerator, RealType>::has_fixed_range;
  161. #endif
  162. template<class UniformRandomNumberGenerator, bool is_number = std::numeric_limits<UniformRandomNumberGenerator>::is_specialized>
  163. struct select_uniform_01
  164. {
  165. template<class RealType>
  166. struct apply
  167. {
  168. typedef backward_compatible_uniform_01<UniformRandomNumberGenerator, RealType> type;
  169. };
  170. };
  171. template<class Num>
  172. struct select_uniform_01<Num, true>
  173. {
  174. template<class RealType>
  175. struct apply
  176. {
  177. typedef new_uniform_01<Num> type;
  178. };
  179. };
  180. }
  181. // Because it is so commonly used: uniform distribution on the real [0..1)
  182. // range. This allows for specializations to avoid a costly int -> float
  183. // conversion plus float multiplication
  184. template<class UniformRandomNumberGenerator = double, class RealType = double>
  185. class uniform_01
  186. : public detail::select_uniform_01<UniformRandomNumberGenerator>::BOOST_NESTED_TEMPLATE apply<RealType>::type
  187. {
  188. typedef typename detail::select_uniform_01<UniformRandomNumberGenerator>::BOOST_NESTED_TEMPLATE apply<RealType>::type impl_type;
  189. typedef boost::random::detail::ptr_helper<UniformRandomNumberGenerator> traits;
  190. public:
  191. uniform_01() {}
  192. explicit uniform_01(typename traits::rvalue_type rng)
  193. : impl_type(rng)
  194. {
  195. }
  196. #if !defined(BOOST_NO_OPERATORS_IN_NAMESPACE) && !defined(BOOST_NO_MEMBER_TEMPLATE_FRIENDS)
  197. template<class CharT, class Traits>
  198. friend std::basic_ostream<CharT,Traits>&
  199. operator<<(std::basic_ostream<CharT,Traits>& os, const uniform_01& u)
  200. {
  201. os << static_cast<const impl_type&>(u);
  202. return os;
  203. }
  204. template<class CharT, class Traits>
  205. friend std::basic_istream<CharT,Traits>&
  206. operator>>(std::basic_istream<CharT,Traits>& is, uniform_01& u)
  207. {
  208. is >> static_cast<impl_type&>(u);
  209. return is;
  210. }
  211. #endif
  212. };
  213. #endif
  214. } // namespace random
  215. using random::uniform_01;
  216. } // namespace boost
  217. #include <boost/random/detail/enable_warnings.hpp>
  218. #endif // BOOST_RANDOM_UNIFORM_01_HPP