1 /* boost random/normal_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: normal_distribution.hpp 60755 2010-03-22 00:45:06Z steven_watanabe $
13 * 2001-02-18 moved to individual header files
16 #ifndef BOOST_RANDOM_NORMAL_DISTRIBUTION_HPP
17 #define BOOST_RANDOM_NORMAL_DISTRIBUTION_HPP
19 #include <boost/config/no_tr1/cmath.hpp>
22 #include <boost/limits.hpp>
23 #include <boost/static_assert.hpp>
24 #include <boost/random/detail/config.hpp>
29 * Instantiations of class template normal_distribution model a
30 * \random_distribution. Such a distribution produces random numbers
31 * @c x distributed with probability density function
32 * \f$p(x) = \frac{1}{\sqrt{2\pi\sigma}} e^{-\frac{(x-\mu)^2}{2\sigma^2}}\f$,
33 * where mean and sigma are the parameters of the distribution.
35 // deterministic Box-Muller method, uses trigonometric functions
36 template<class RealType = double>
37 class normal_distribution
40 typedef RealType input_type;
41 typedef RealType result_type;
43 #if !defined(BOOST_NO_LIMITS_COMPILE_TIME_CONSTANTS) && !(defined(BOOST_MSVC) && BOOST_MSVC <= 1300)
44 BOOST_STATIC_ASSERT(!std::numeric_limits<RealType>::is_integer);
48 * Constructs a normal_distribution object. @c mean and @c sigma are
49 * the parameters for the distribution.
53 explicit normal_distribution(const result_type& mean_arg = result_type(0),
54 const result_type& sigma_arg = result_type(1))
55 : _mean(mean_arg), _sigma(sigma_arg), _valid(false)
57 assert(_sigma >= result_type(0));
60 // compiler-generated copy constructor is NOT fine, need to purge cache
61 normal_distribution(const normal_distribution& other)
62 : _mean(other._mean), _sigma(other._sigma), _valid(false)
66 // compiler-generated copy ctor and assignment operator are fine
69 * Returns: The "mean" parameter of the distribution.
71 RealType mean() const { return _mean; }
73 * Returns: The "sigma" parameter of the distribution.
75 RealType sigma() const { return _sigma; }
77 void reset() { _valid = false; }
79 template<class Engine>
80 result_type operator()(Engine& eng)
82 #ifndef BOOST_NO_STDC_NAMESPACE
83 // allow for Koenig lookup
84 using std::sqrt; using std::log; using std::sin; using std::cos;
89 _cached_rho = sqrt(-result_type(2) * log(result_type(1)-_r2));
94 // Can we have a boost::mathconst please?
95 const result_type pi = result_type(3.14159265358979323846);
97 return _cached_rho * (_valid ?
98 cos(result_type(2)*pi*_r1) :
99 sin(result_type(2)*pi*_r1))
103 #ifndef BOOST_RANDOM_NO_STREAM_OPERATORS
104 template<class CharT, class Traits>
105 friend std::basic_ostream<CharT,Traits>&
106 operator<<(std::basic_ostream<CharT,Traits>& os, const normal_distribution& nd)
108 os << nd._mean << " " << nd._sigma << " "
109 << nd._valid << " " << nd._cached_rho << " " << nd._r1;
113 template<class CharT, class Traits>
114 friend std::basic_istream<CharT,Traits>&
115 operator>>(std::basic_istream<CharT,Traits>& is, normal_distribution& nd)
117 is >> std::ws >> nd._mean >> std::ws >> nd._sigma
118 >> std::ws >> nd._valid >> std::ws >> nd._cached_rho
119 >> std::ws >> nd._r1;
124 result_type _mean, _sigma;
125 result_type _r1, _r2, _cached_rho;
131 #endif // BOOST_RANDOM_NORMAL_DISTRIBUTION_HPP