uniform_int_float.hpp 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. /* boost random/detail/uniform_int_float.hpp header file
  2. *
  3. * Copyright Jens Maurer 2000-2001
  4. * Copyright Steven Watanabe 2011
  5. * Distributed under the Boost Software License, Version 1.0. (See
  6. * accompanying file LICENSE_1_0.txt or copy at
  7. * http://www.boost.org/LICENSE_1_0.txt)
  8. *
  9. * See http://www.boost.org for most recent version including documentation.
  10. *
  11. * $Id$
  12. *
  13. */
  14. #ifndef BOOST_RANDOM_DETAIL_UNIFORM_INT_FLOAT_HPP
  15. #define BOOST_RANDOM_DETAIL_UNIFORM_INT_FLOAT_HPP
  16. #include <boost/limits.hpp>
  17. #include <boost/config.hpp>
  18. #include <boost/integer.hpp>
  19. #include <boost/random/detail/config.hpp>
  20. #include <boost/random/detail/generator_bits.hpp>
  21. #include <boost/random/detail/disable_warnings.hpp>
  22. namespace boost {
  23. namespace random {
  24. namespace detail {
  25. template<class URNG>
  26. class uniform_int_float
  27. {
  28. public:
  29. typedef URNG base_type;
  30. typedef typename base_type::result_type base_result;
  31. typedef typename boost::uint_t<
  32. (std::numeric_limits<boost::uintmax_t>::digits <
  33. std::numeric_limits<base_result>::digits)?
  34. std::numeric_limits<boost::uintmax_t>::digits :
  35. std::numeric_limits<base_result>::digits
  36. >::fast result_type;
  37. uniform_int_float(base_type& rng)
  38. : _rng(rng) {}
  39. static result_type min BOOST_PREVENT_MACRO_SUBSTITUTION ()
  40. { return 0; }
  41. static result_type max BOOST_PREVENT_MACRO_SUBSTITUTION ()
  42. {
  43. std::size_t digits = std::numeric_limits<result_type>::digits;
  44. if(detail::generator_bits<URNG>::value() < digits) {
  45. digits = detail::generator_bits<URNG>::value();
  46. }
  47. return (result_type(2) << (digits - 1)) - 1;
  48. }
  49. base_type& base() { return _rng; }
  50. const base_type& base() const { return _rng; }
  51. result_type operator()()
  52. {
  53. base_result range = static_cast<base_result>((max)())+1;
  54. return static_cast<result_type>(_rng() * range);
  55. }
  56. private:
  57. base_type& _rng;
  58. };
  59. } // namespace detail
  60. } // namespace random
  61. } // namespace boost
  62. #include <boost/random/detail/enable_warnings.hpp>
  63. #endif // BOOST_RANDOM_DETAIL_UNIFORM_INT_FLOAT_HPP