fix doc example typo
[boost.git] / boost / random / triangle_distribution.hpp
blob0c330d94f0c22c431ddec420a03dc8d7493f75f4
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$
12 * Revision history
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>
20 #include <cassert>
21 #include <boost/random/detail/config.hpp>
22 #include <boost/random/uniform_01.hpp>
24 namespace boost {
26 // triangle distribution, with a smallest, b most probable, and c largest
27 // value.
28 template<class RealType = double>
29 class triangle_distribution
31 public:
32 typedef RealType input_type;
33 typedef RealType result_type;
35 explicit triangle_distribution(result_type a_arg = result_type(0),
36 result_type b_arg = result_type(0.5),
37 result_type c_arg = result_type(1))
38 : _a(a_arg), _b(b_arg), _c(c_arg)
40 assert(_a <= _b && _b <= _c);
41 init();
44 // compiler-generated copy ctor and assignment operator are fine
45 result_type a() const { return _a; }
46 result_type b() const { return _b; }
47 result_type c() const { return _c; }
49 void reset() { }
51 template<class Engine>
52 result_type operator()(Engine& eng)
54 #ifndef BOOST_NO_STDC_NAMESPACE
55 using std::sqrt;
56 #endif
57 result_type u = eng();
58 if( u <= q1 )
59 return _a + p1*sqrt(u);
60 else
61 return _c - d3*sqrt(d2*u-d1);
64 #ifndef BOOST_RANDOM_NO_STREAM_OPERATORS
65 template<class CharT, class Traits>
66 friend std::basic_ostream<CharT,Traits>&
67 operator<<(std::basic_ostream<CharT,Traits>& os, const triangle_distribution& td)
69 os << td._a << " " << td._b << " " << td._c;
70 return os;
73 template<class CharT, class Traits>
74 friend std::basic_istream<CharT,Traits>&
75 operator>>(std::basic_istream<CharT,Traits>& is, triangle_distribution& td)
77 is >> std::ws >> td._a >> std::ws >> td._b >> std::ws >> td._c;
78 td.init();
79 return is;
81 #endif
83 private:
84 void init()
86 #ifndef BOOST_NO_STDC_NAMESPACE
87 using std::sqrt;
88 #endif
89 d1 = _b - _a;
90 d2 = _c - _a;
91 d3 = sqrt(_c - _b);
92 q1 = d1 / d2;
93 p1 = sqrt(d1 * d2);
96 result_type _a, _b, _c;
97 result_type d1, d2, d3, q1, p1;
100 } // namespace boost
102 #endif // BOOST_RANDOM_TRIANGLE_DISTRIBUTION_HPP