1 /* boost random/normal_distribution.hpp header file
\r
3 * Copyright Jens Maurer 2000-2001
\r
4 * Distributed under the Boost Software License, Version 1.0. (See
\r
5 * accompanying file LICENSE_1_0.txt or copy at
\r
6 * http://www.boost.org/LICENSE_1_0.txt)
\r
8 * See http://www.boost.org for most recent version including documentation.
\r
10 * $Id: normal_distribution.hpp 52492 2009-04-19 14:55:57Z steven_watanabe $
\r
13 * 2001-02-18 moved to individual header files
\r
16 #ifndef BOOST_RANDOM_NORMAL_DISTRIBUTION_HPP
\r
17 #define BOOST_RANDOM_NORMAL_DISTRIBUTION_HPP
\r
19 #include <boost/config/no_tr1/cmath.hpp>
\r
22 #include <boost/limits.hpp>
\r
23 #include <boost/static_assert.hpp>
\r
24 #include <boost/random/detail/config.hpp>
\r
28 // deterministic Box-Muller method, uses trigonometric functions
\r
29 template<class RealType = double>
\r
30 class normal_distribution
\r
33 typedef RealType input_type;
\r
34 typedef RealType result_type;
\r
36 #if !defined(BOOST_NO_LIMITS_COMPILE_TIME_CONSTANTS) && !(defined(BOOST_MSVC) && BOOST_MSVC <= 1300)
\r
37 BOOST_STATIC_ASSERT(!std::numeric_limits<RealType>::is_integer);
\r
40 explicit normal_distribution(const result_type& mean_arg = result_type(0),
\r
41 const result_type& sigma_arg = result_type(1))
\r
42 : _mean(mean_arg), _sigma(sigma_arg), _valid(false)
\r
44 assert(_sigma >= result_type(0));
\r
47 // compiler-generated copy constructor is NOT fine, need to purge cache
\r
48 normal_distribution(const normal_distribution& other)
\r
49 : _mean(other._mean), _sigma(other._sigma), _valid(false)
\r
53 // compiler-generated copy ctor and assignment operator are fine
\r
55 RealType mean() const { return _mean; }
\r
56 RealType sigma() const { return _sigma; }
\r
58 void reset() { _valid = false; }
\r
60 template<class Engine>
\r
61 result_type operator()(Engine& eng)
\r
63 #ifndef BOOST_NO_STDC_NAMESPACE
\r
64 // allow for Koenig lookup
\r
65 using std::sqrt; using std::log; using std::sin; using std::cos;
\r
70 _cached_rho = sqrt(-result_type(2) * log(result_type(1)-_r2));
\r
75 // Can we have a boost::mathconst please?
\r
76 const result_type pi = result_type(3.14159265358979323846);
\r
78 return _cached_rho * (_valid ?
\r
79 cos(result_type(2)*pi*_r1) :
\r
80 sin(result_type(2)*pi*_r1))
\r
84 #ifndef BOOST_RANDOM_NO_STREAM_OPERATORS
\r
85 template<class CharT, class Traits>
\r
86 friend std::basic_ostream<CharT,Traits>&
\r
87 operator<<(std::basic_ostream<CharT,Traits>& os, const normal_distribution& nd)
\r
89 os << nd._mean << " " << nd._sigma << " "
\r
90 << nd._valid << " " << nd._cached_rho << " " << nd._r1;
\r
94 template<class CharT, class Traits>
\r
95 friend std::basic_istream<CharT,Traits>&
\r
96 operator>>(std::basic_istream<CharT,Traits>& is, normal_distribution& nd)
\r
98 is >> std::ws >> nd._mean >> std::ws >> nd._sigma
\r
99 >> std::ws >> nd._valid >> std::ws >> nd._cached_rho
\r
100 >> std::ws >> nd._r1;
\r
105 result_type _mean, _sigma;
\r
106 result_type _r1, _r2, _cached_rho;
\r
110 } // namespace boost
\r
112 #endif // BOOST_RANDOM_NORMAL_DISTRIBUTION_HPP
\r