generate_canonical.hpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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$
  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/mpl/bool.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. std::size_t digits = std::numeric_limits<RealType>::digits;
  53. std::size_t engine_bits = detail::generator_bits<URNG>::value();
  54. std::size_t b = (std::min)(bits, digits);
  55. RealType R = pow(RealType(2), RealType(engine_bits));
  56. RealType mult = R;
  57. RealType limit = pow(RealType(2), RealType(b));
  58. RealType S = RealType(g() - (g.min)());
  59. while(mult < limit) {
  60. RealType inc(floor((RealType(g()) - RealType((g.min)())) * R));
  61. S += inc * mult;
  62. mult *= R;
  63. }
  64. return S / mult;
  65. }
  66. }
  67. /**
  68. * Returns a value uniformly distributed in the range [0, 1)
  69. * with at least @c bits random bits.
  70. */
  71. template<class RealType, std::size_t bits, class URNG>
  72. RealType generate_canonical(URNG& g)
  73. {
  74. RealType result = detail::generate_canonical_impl<RealType, bits>(
  75. g, boost::random::traits::is_integral<typename URNG::result_type>());
  76. BOOST_ASSERT(result >= 0);
  77. BOOST_ASSERT(result <= 1);
  78. if(result == 1) {
  79. result -= std::numeric_limits<RealType>::epsilon() / 2;
  80. BOOST_ASSERT(result != 1);
  81. }
  82. return result;
  83. }
  84. } // namespace random
  85. } // namespace boost
  86. #endif // BOOST_RANDOM_GENERATE_CANONICAL_HPP