]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
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 | ||
11 | #ifndef BOOST_COMPUTE_RANDOM_UNIFORM_REAL_DISTRIBUTION_HPP | |
12 | #define BOOST_COMPUTE_RANDOM_UNIFORM_REAL_DISTRIBUTION_HPP | |
13 | ||
14 | #include <boost/assert.hpp> | |
15 | #include <boost/type_traits.hpp> | |
16 | ||
17 | #include <boost/compute/command_queue.hpp> | |
18 | #include <boost/compute/function.hpp> | |
19 | #include <boost/compute/detail/literal.hpp> | |
20 | #include <boost/compute/types/fundamental.hpp> | |
21 | ||
22 | namespace boost { | |
23 | namespace compute { | |
24 | ||
25 | /// \class uniform_real_distribution | |
26 | /// \brief Produces uniformly distributed random floating-point numbers. | |
27 | /// | |
28 | /// The following example shows how to setup a uniform real distribution to | |
29 | /// produce random \c float values between \c 1 and \c 100. | |
30 | /// | |
31 | /// \snippet test/test_uniform_real_distribution.cpp generate | |
32 | /// | |
33 | /// \see default_random_engine, normal_distribution | |
34 | template<class RealType = float> | |
35 | class uniform_real_distribution | |
36 | { | |
37 | public: | |
38 | typedef RealType result_type; | |
39 | ||
40 | /// Creates a new uniform distribution producing numbers in the range | |
41 | /// [\p a, \p b). | |
42 | /// Requires a < b | |
43 | uniform_real_distribution(RealType a = 0.f, RealType b = 1.f) | |
44 | : m_a(a), | |
45 | m_b(b) | |
46 | { | |
47 | BOOST_ASSERT(a < b); | |
48 | } | |
49 | ||
50 | /// Destroys the uniform_real_distribution object. | |
51 | ~uniform_real_distribution() | |
52 | { | |
53 | } | |
54 | ||
55 | /// Returns the minimum value of the distribution. | |
56 | result_type a() const | |
57 | { | |
58 | return m_a; | |
59 | } | |
60 | ||
61 | /// Returns the maximum value of the distribution. | |
62 | result_type b() const | |
63 | { | |
64 | return m_b; | |
65 | } | |
66 | ||
67 | /// Generates uniformly distributed floating-point numbers and stores | |
68 | /// them to the range [\p first, \p last). | |
69 | template<class OutputIterator, class Generator> | |
70 | void generate(OutputIterator first, | |
71 | OutputIterator last, | |
72 | Generator &generator, | |
73 | command_queue &queue) | |
74 | { | |
75 | BOOST_COMPUTE_FUNCTION(RealType, scale_random, (const uint_ x), | |
76 | { | |
77 | return nextafter(LO + (convert_RealType(x) / MAX_RANDOM) * (HI - LO), (RealType) LO); | |
78 | }); | |
79 | ||
80 | scale_random.define("LO", detail::make_literal(m_a)); | |
81 | scale_random.define("HI", detail::make_literal(m_b)); | |
82 | scale_random.define("MAX_RANDOM", "UINT_MAX"); | |
83 | scale_random.define( | |
84 | "convert_RealType", std::string("convert_") + type_name<RealType>() | |
85 | ); | |
86 | scale_random.define("RealType", type_name<RealType>()); | |
87 | ||
88 | generator.generate( | |
89 | first, last, scale_random, queue | |
90 | ); | |
91 | } | |
92 | ||
93 | /// \internal_ (deprecated) | |
94 | template<class OutputIterator, class Generator> | |
95 | void fill(OutputIterator first, | |
96 | OutputIterator last, | |
97 | Generator &g, | |
98 | command_queue &queue) | |
99 | { | |
100 | generate(first, last, g, queue); | |
101 | } | |
102 | ||
103 | private: | |
104 | RealType m_a; | |
105 | RealType m_b; | |
106 | ||
107 | BOOST_STATIC_ASSERT_MSG( | |
108 | boost::is_floating_point<RealType>::value, | |
109 | "Template argument must be a floating point type" | |
110 | ); | |
111 | }; | |
112 | ||
113 | } // end compute namespace | |
114 | } // end boost namespace | |
115 | ||
116 | #endif // BOOST_COMPUTE_RANDOM_UNIFORM_REAL_DISTRIBUTION_HPP |