1 //---------------------------------------------------------------------------//
2 // Copyright (c) 2014 Roshan <thisisroshansmail@gmail.com>
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
8 // See http://boostorg.github.com/compute for more information.
9 //---------------------------------------------------------------------------//
11 #ifndef BOOST_COMPUTE_RANDOM_BERNOULLI_DISTRIBUTION_HPP
12 #define BOOST_COMPUTE_RANDOM_BERNOULLI_DISTRIBUTION_HPP
14 #include <boost/assert.hpp>
15 #include <boost/type_traits.hpp>
17 #include <boost/compute/command_queue.hpp>
18 #include <boost/compute/function.hpp>
19 #include <boost/compute/types/fundamental.hpp>
20 #include <boost/compute/detail/iterator_range_size.hpp>
21 #include <boost/compute/detail/literal.hpp>
27 /// \class bernoulli_distribution
28 /// \brief Produces random boolean values according to the following
29 /// discrete probability function with parameter p :
30 /// P(true/p) = p and P(false/p) = (1 - p)
32 /// The following example shows how to setup a bernoulli distribution to
33 /// produce random boolean values with parameter p = 0.25
35 /// \snippet test/test_bernoulli_distribution.cpp generate
37 template<class RealType = float>
38 class bernoulli_distribution
42 /// Creates a new bernoulli distribution
43 bernoulli_distribution(RealType p = 0.5f)
48 /// Destroys the bernoulli_distribution object
49 ~bernoulli_distribution()
53 /// Returns the value of the parameter p
59 /// Generates bernoulli distributed booleans and stores
60 /// them in the range [\p first, \p last).
61 template<class OutputIterator, class Generator>
62 void generate(OutputIterator first,
67 size_t count = detail::iterator_range_size(first, last);
69 vector<uint_> tmp(count, queue.get_context());
70 generator.generate(tmp.begin(), tmp.end(), queue);
72 BOOST_COMPUTE_FUNCTION(bool, scale_random, (const uint_ x),
74 return (convert_RealType(x) / MAX_RANDOM) < PARAM;
77 scale_random.define("PARAM", detail::make_literal(m_p));
78 scale_random.define("MAX_RANDOM", "UINT_MAX");
80 "convert_RealType", std::string("convert_") + type_name<RealType>()
84 tmp.begin(), tmp.end(), first, scale_random, queue
91 BOOST_STATIC_ASSERT_MSG(
92 boost::is_floating_point<RealType>::value,
93 "Template argument must be a floating point type"
97 } // end compute namespace
98 } // end boost namespace
100 #endif // BOOST_COMPUTE_RANDOM_BERNOULLI_DISTRIBUTION_HPP