uniform_real_distribution.hpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. //---------------------------------------------------------------------------//
  2. // Copyright (c) 2013 Kyle Lutz <kyle.r.lutz@gmail.com>
  3. //
  4. // Distributed under the Boost Software License, Version 1.0
  5. // See accompanying file LICENSE_1_0.txt or copy at
  6. // http://www.boost.org/LICENSE_1_0.txt
  7. //
  8. // See http://boostorg.github.com/compute for more information.
  9. //---------------------------------------------------------------------------//
  10. #ifndef BOOST_COMPUTE_RANDOM_UNIFORM_REAL_DISTRIBUTION_HPP
  11. #define BOOST_COMPUTE_RANDOM_UNIFORM_REAL_DISTRIBUTION_HPP
  12. #include <boost/assert.hpp>
  13. #include <boost/type_traits.hpp>
  14. #include <boost/compute/command_queue.hpp>
  15. #include <boost/compute/function.hpp>
  16. #include <boost/compute/detail/literal.hpp>
  17. #include <boost/compute/types/fundamental.hpp>
  18. namespace boost {
  19. namespace compute {
  20. /// \class uniform_real_distribution
  21. /// \brief Produces uniformly distributed random floating-point numbers.
  22. ///
  23. /// The following example shows how to setup a uniform real distribution to
  24. /// produce random \c float values between \c 1 and \c 100.
  25. ///
  26. /// \snippet test/test_uniform_real_distribution.cpp generate
  27. ///
  28. /// \see default_random_engine, normal_distribution
  29. template<class RealType = float>
  30. class uniform_real_distribution
  31. {
  32. public:
  33. typedef RealType result_type;
  34. /// Creates a new uniform distribution producing numbers in the range
  35. /// [\p a, \p b).
  36. /// Requires a < b
  37. uniform_real_distribution(RealType a = 0.f, RealType b = 1.f)
  38. : m_a(a),
  39. m_b(b)
  40. {
  41. BOOST_ASSERT(a < b);
  42. }
  43. /// Destroys the uniform_real_distribution object.
  44. ~uniform_real_distribution()
  45. {
  46. }
  47. /// Returns the minimum value of the distribution.
  48. result_type a() const
  49. {
  50. return m_a;
  51. }
  52. /// Returns the maximum value of the distribution.
  53. result_type b() const
  54. {
  55. return m_b;
  56. }
  57. /// Generates uniformly distributed floating-point numbers and stores
  58. /// them to the range [\p first, \p last).
  59. template<class OutputIterator, class Generator>
  60. void generate(OutputIterator first,
  61. OutputIterator last,
  62. Generator &generator,
  63. command_queue &queue)
  64. {
  65. BOOST_COMPUTE_FUNCTION(RealType, scale_random, (const uint_ x),
  66. {
  67. return nextafter(LO + (convert_RealType(x) / MAX_RANDOM) * (HI - LO), (RealType) LO);
  68. });
  69. scale_random.define("LO", detail::make_literal(m_a));
  70. scale_random.define("HI", detail::make_literal(m_b));
  71. scale_random.define("MAX_RANDOM", "UINT_MAX");
  72. scale_random.define(
  73. "convert_RealType", std::string("convert_") + type_name<RealType>()
  74. );
  75. scale_random.define("RealType", type_name<RealType>());
  76. generator.generate(
  77. first, last, scale_random, queue
  78. );
  79. }
  80. /// \internal_ (deprecated)
  81. template<class OutputIterator, class Generator>
  82. void fill(OutputIterator first,
  83. OutputIterator last,
  84. Generator &g,
  85. command_queue &queue)
  86. {
  87. generate(first, last, g, queue);
  88. }
  89. private:
  90. RealType m_a;
  91. RealType m_b;
  92. BOOST_STATIC_ASSERT_MSG(
  93. boost::is_floating_point<RealType>::value,
  94. "Template argument must be a floating point type"
  95. );
  96. };
  97. } // end compute namespace
  98. } // end boost namespace
  99. #endif // BOOST_COMPUTE_RANDOM_UNIFORM_REAL_DISTRIBUTION_HPP