/Src/Dependencies/Boost/boost/random/uniform_01.hpp

http://hadesmem.googlecode.com/ · C++ Header · 277 lines · 203 code · 41 blank · 33 comment · 8 complexity · c709c956a9f24dfefc96d258547ed4aa MD5 · raw file

  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: uniform_01.hpp 71018 2011-04-05 21:27:52Z steven_watanabe $
  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((eng.max)()-(eng.min)()) +
  84. result_type(std::numeric_limits<base_result>::is_integer ? 1 : 0));
  85. result_type result = result_type(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) && !(defined(BOOST_MSVC) && BOOST_MSVC <= 1300)
  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>
  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<>
  172. struct select_uniform_01<float>
  173. {
  174. template<class RealType>
  175. struct apply
  176. {
  177. typedef new_uniform_01<float> type;
  178. };
  179. };
  180. template<>
  181. struct select_uniform_01<double>
  182. {
  183. template<class RealType>
  184. struct apply
  185. {
  186. typedef new_uniform_01<double> type;
  187. };
  188. };
  189. template<>
  190. struct select_uniform_01<long double>
  191. {
  192. template<class RealType>
  193. struct apply
  194. {
  195. typedef new_uniform_01<long double> type;
  196. };
  197. };
  198. }
  199. // Because it is so commonly used: uniform distribution on the real [0..1)
  200. // range. This allows for specializations to avoid a costly int -> float
  201. // conversion plus float multiplication
  202. template<class UniformRandomNumberGenerator = double, class RealType = double>
  203. class uniform_01
  204. : public detail::select_uniform_01<UniformRandomNumberGenerator>::BOOST_NESTED_TEMPLATE apply<RealType>::type
  205. {
  206. typedef typename detail::select_uniform_01<UniformRandomNumberGenerator>::BOOST_NESTED_TEMPLATE apply<RealType>::type impl_type;
  207. typedef boost::random::detail::ptr_helper<UniformRandomNumberGenerator> traits;
  208. public:
  209. uniform_01() {}
  210. explicit uniform_01(typename traits::rvalue_type rng)
  211. : impl_type(rng)
  212. {
  213. }
  214. #if !defined(BOOST_NO_OPERATORS_IN_NAMESPACE) && !defined(BOOST_NO_MEMBER_TEMPLATE_FRIENDS)
  215. template<class CharT, class Traits>
  216. friend std::basic_ostream<CharT,Traits>&
  217. operator<<(std::basic_ostream<CharT,Traits>& os, const uniform_01& u)
  218. {
  219. os << static_cast<const impl_type&>(u);
  220. return os;
  221. }
  222. template<class CharT, class Traits>
  223. friend std::basic_istream<CharT,Traits>&
  224. operator>>(std::basic_istream<CharT,Traits>& is, uniform_01& u)
  225. {
  226. is >> static_cast<impl_type&>(u);
  227. return is;
  228. }
  229. #endif
  230. };
  231. #endif
  232. } // namespace random
  233. using random::uniform_01;
  234. } // namespace boost
  235. #include <boost/random/detail/enable_warnings.hpp>
  236. #endif // BOOST_RANDOM_UNIFORM_01_HPP