1 /* boost random/triangle_distribution.hpp header file
3 * Copyright Jens Maurer 2000-2001
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)
8 * See http://www.boost.org for most recent version including documentation.
10 * $Id: triangle_distribution.hpp 60755 2010-03-22 00:45:06Z steven_watanabe $
13 * 2001-02-18 moved to individual header files
16 #ifndef BOOST_RANDOM_TRIANGLE_DISTRIBUTION_HPP
17 #define BOOST_RANDOM_TRIANGLE_DISTRIBUTION_HPP
19 #include <boost/config/no_tr1/cmath.hpp>
21 #include <boost/random/detail/config.hpp>
22 #include <boost/random/uniform_01.hpp>
27 * Instantiations of @c triangle_distribution model a \random_distribution.
28 * A @c triangle_distribution has three parameters, @c a, @c b, and @c c,
29 * which are the smallest, the most probable and the largest values of
30 * the distribution respectively.
32 template<class RealType = double>
33 class triangle_distribution
36 typedef RealType input_type;
37 typedef RealType result_type;
40 * Constructs a @c triangle_distribution with the parameters
41 * @c a, @c b, and @c c.
43 * Preconditions: a <= b <= c.
45 explicit triangle_distribution(result_type a_arg = result_type(0),
46 result_type b_arg = result_type(0.5),
47 result_type c_arg = result_type(1))
48 : _a(a_arg), _b(b_arg), _c(c_arg)
50 assert(_a <= _b && _b <= _c);
54 // compiler-generated copy ctor and assignment operator are fine
56 /** Returns the @c a parameter of the distribution */
57 result_type a() const { return _a; }
58 /** Returns the @c b parameter of the distribution */
59 result_type b() const { return _b; }
60 /** Returns the @c c parameter of the distribution */
61 result_type c() const { return _c; }
65 template<class Engine>
66 result_type operator()(Engine& eng)
68 #ifndef BOOST_NO_STDC_NAMESPACE
71 result_type u = eng();
73 return _a + p1*sqrt(u);
75 return _c - d3*sqrt(d2*u-d1);
78 #ifndef BOOST_RANDOM_NO_STREAM_OPERATORS
79 template<class CharT, class Traits>
80 friend std::basic_ostream<CharT,Traits>&
81 operator<<(std::basic_ostream<CharT,Traits>& os, const triangle_distribution& td)
83 os << td._a << " " << td._b << " " << td._c;
87 template<class CharT, class Traits>
88 friend std::basic_istream<CharT,Traits>&
89 operator>>(std::basic_istream<CharT,Traits>& is, triangle_distribution& td)
91 is >> std::ws >> td._a >> std::ws >> td._b >> std::ws >> td._c;
98 /// \cond hide_private_members
101 #ifndef BOOST_NO_STDC_NAMESPACE
112 result_type _a, _b, _c;
113 result_type d1, d2, d3, q1, p1;
118 #endif // BOOST_RANDOM_TRIANGLE_DISTRIBUTION_HPP