generate_canonical.hpp 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /* boost random/generate_canonical.hpp header file
  2. *
  3. * Copyright Steven Watanabe 2011
  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: generate_canonical.hpp 72951 2011-07-07 04:57:37Z steven_watanabe $
  11. *
  12. */
  13. #ifndef BOOST_RANDOM_GENERATE_CANONICAL_HPP
  14. #define BOOST_RANDOM_GENERATE_CANONICAL_HPP
  15. #include <algorithm>
  16. #include <boost/assert.hpp>
  17. #include <boost/config/no_tr1/cmath.hpp>
  18. #include <boost/limits.hpp>
  19. #include <boost/type_traits/is_integral.hpp>
  20. #include <boost/math/special_functions.hpp>
  21. #include <boost/random/detail/signed_unsigned_tools.hpp>
  22. #include <boost/random/detail/generator_bits.hpp>
  23. namespace boost {
  24. namespace random {
  25. namespace detail {
  26. template<class RealType, std::size_t bits, class URNG>
  27. RealType generate_canonical_impl(URNG& g, boost::mpl::true_ /*is_integral*/)
  28. {
  29. using std::pow;
  30. typedef typename URNG::result_type base_result;
  31. std::size_t digits = std::numeric_limits<RealType>::digits;
  32. RealType R = RealType((g.max)()) - RealType((g.min)()) + 1;
  33. RealType mult = R;
  34. RealType limit =
  35. pow(RealType(2),
  36. RealType((std::min)(static_cast<std::size_t>(bits), digits)));
  37. RealType S = RealType(detail::subtract<base_result>()(g(), (g.min)()));
  38. while(mult < limit) {
  39. RealType inc = RealType(detail::subtract<base_result>()(g(), (g.min)()));
  40. S += inc * mult;
  41. mult *= R;
  42. }
  43. return S / mult;
  44. }
  45. template<class RealType, std::size_t bits, class URNG>
  46. RealType generate_canonical_impl(URNG& g, boost::mpl::false_ /*is_integral*/)
  47. {
  48. using std::pow;
  49. using std::floor;
  50. BOOST_ASSERT((g.min)() == 0);
  51. BOOST_ASSERT((g.max)() == 1);
  52. typedef typename URNG::result_type base_result;
  53. std::size_t digits = std::numeric_limits<RealType>::digits;
  54. std::size_t engine_bits = detail::generator_bits<URNG>::value();
  55. std::size_t b = (std::min)(bits, digits);
  56. RealType R = pow(RealType(2), RealType(engine_bits));
  57. RealType mult = R;
  58. RealType limit = pow(RealType(2), RealType(b));
  59. RealType S = RealType(g() - (g.min)());
  60. while(mult < limit) {
  61. RealType inc(floor((RealType(g()) - RealType((g.min)())) * R));
  62. S += inc * mult;
  63. mult *= R;
  64. }
  65. return S / mult;
  66. }
  67. }
  68. /**
  69. * Returns a value uniformly distributed in the range [0, 1)
  70. * with at least @c bits random bits.
  71. */
  72. template<class RealType, std::size_t bits, class URNG>
  73. RealType generate_canonical(URNG& g)
  74. {
  75. RealType result = detail::generate_canonical_impl<RealType, bits>(
  76. g, boost::is_integral<typename URNG::result_type>());
  77. BOOST_ASSERT(result >= 0);
  78. BOOST_ASSERT(result <= 1);
  79. if(result == 1) {
  80. result -= std::numeric_limits<RealType>::epsilon() / 2;
  81. BOOST_ASSERT(result != 1);
  82. }
  83. return result;
  84. }
  85. } // namespace random
  86. } // namespace boost
  87. #endif // BOOST_RANDOM_GENERATE_CANONICAL_HPP