generate_canonical.hpp 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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/random/detail/signed_unsigned_tools.hpp>
  21. #include <boost/random/detail/generator_bits.hpp>
  22. namespace boost {
  23. namespace random {
  24. namespace detail {
  25. template<class RealType, std::size_t bits, class URNG>
  26. RealType generate_canonical_impl(URNG& g, boost::true_type /*is_integral*/)
  27. {
  28. using std::pow;
  29. typedef typename URNG::result_type base_result;
  30. std::size_t digits = std::numeric_limits<RealType>::digits;
  31. RealType R = RealType((g.max)()) - RealType((g.min)()) + 1;
  32. RealType mult = R;
  33. RealType limit =
  34. pow(RealType(2),
  35. RealType((std::min)(static_cast<std::size_t>(bits), digits)));
  36. RealType S = RealType(detail::subtract<base_result>()(g(), (g.min)()));
  37. while(mult < limit) {
  38. RealType inc = RealType(detail::subtract<base_result>()(g(), (g.min)()));
  39. S += inc * mult;
  40. mult *= R;
  41. }
  42. return S / mult;
  43. }
  44. template<class RealType, std::size_t bits, class URNG>
  45. RealType generate_canonical_impl(URNG& g, boost::false_type /*is_integral*/)
  46. {
  47. using std::pow;
  48. using std::floor;
  49. BOOST_ASSERT((g.min)() == 0);
  50. BOOST_ASSERT((g.max)() == 1);
  51. std::size_t digits = std::numeric_limits<RealType>::digits;
  52. std::size_t engine_bits = detail::generator_bits<URNG>::value();
  53. std::size_t b = (std::min)(bits, digits);
  54. RealType R = pow(RealType(2), RealType(engine_bits));
  55. RealType mult = R;
  56. RealType limit = pow(RealType(2), RealType(b));
  57. RealType S = RealType(g() - (g.min)());
  58. while(mult < limit) {
  59. RealType inc(floor((RealType(g()) - RealType((g.min)())) * R));
  60. S += inc * mult;
  61. mult *= R;
  62. }
  63. return S / mult;
  64. }
  65. }
  66. /**
  67. * Returns a value uniformly distributed in the range [0, 1)
  68. * with at least @c bits random bits.
  69. */
  70. template<class RealType, std::size_t bits, class URNG>
  71. RealType generate_canonical(URNG& g)
  72. {
  73. RealType result = detail::generate_canonical_impl<RealType, bits>(
  74. g, boost::random::traits::is_integral<typename URNG::result_type>());
  75. BOOST_ASSERT(result >= 0);
  76. BOOST_ASSERT(result <= 1);
  77. if(result == 1) {
  78. result -= std::numeric_limits<RealType>::epsilon() / 2;
  79. BOOST_ASSERT(result != 1);
  80. }
  81. return result;
  82. }
  83. } // namespace random
  84. } // namespace boost
  85. #endif // BOOST_RANDOM_GENERATE_CANONICAL_HPP