- // Can we have a boost::mathconst please?
- const result_type pi = result_type(3.14159265358979323846);
-
- return _cached_rho * (_valid ?
- cos(result_type(2)*pi*_r1) :
- sin(result_type(2)*pi*_r1))
- * _sigma + _mean;
- }
-
-#ifndef BOOST_RANDOM_NO_STREAM_OPERATORS
- template<class CharT, class Traits>
- friend std::basic_ostream<CharT,Traits>&
- operator<<(std::basic_ostream<CharT,Traits>& os, const normal_distribution& nd)
- {
- os << nd._mean << " " << nd._sigma << " "
- << nd._valid << " " << nd._cached_rho << " " << nd._r1;
- return os;
- }
-
- template<class CharT, class Traits>
- friend std::basic_istream<CharT,Traits>&
- operator>>(std::basic_istream<CharT,Traits>& is, normal_distribution& nd)
- {
- is >> std::ws >> nd._mean >> std::ws >> nd._sigma
- >> std::ws >> nd._valid >> std::ws >> nd._cached_rho
- >> std::ws >> nd._r1;
- return is;
- }
-#endif
+
+ /**
+ * Constructs a @c normal_distribution object from its parameters.
+ */
+ explicit normal_distribution(const param_type& parm)
+ : _mean(parm.mean()), _sigma(parm.sigma()),
+ _r1(0), _r2(0), _cached_rho(0), _valid(false)
+ {}
+
+ /** Returns the mean of the distribution. */
+ RealType mean() const { return _mean; }
+ /** Returns the standard deviation of the distribution. */
+ RealType sigma() const { return _sigma; }
+
+ /** Returns the smallest value that the distribution can produce. */
+ RealType min BOOST_PREVENT_MACRO_SUBSTITUTION () const
+ { return -std::numeric_limits<RealType>::infinity(); }
+ /** Returns the largest value that the distribution can produce. */
+ RealType max BOOST_PREVENT_MACRO_SUBSTITUTION () const
+ { return std::numeric_limits<RealType>::infinity(); }
+
+ /** Returns the parameters of the distribution. */
+ param_type param() const { return param_type(_mean, _sigma); }
+ /** Sets the parameters of the distribution. */
+ void param(const param_type& parm)
+ {
+ _mean = parm.mean();
+ _sigma = parm.sigma();
+ _valid = false;
+ }
+
+ /**
+ * Effects: Subsequent uses of the distribution do not depend
+ * on values produced by any engine prior to invoking reset.
+ */
+ void reset() { _valid = false; }
+
+ /** Returns a normal variate. */
+ template<class Engine>
+ result_type operator()(Engine& eng)
+ {
+ using std::sqrt;
+ using std::log;
+ using std::sin;
+ using std::cos;
+
+ if(!_valid) {
+ _r1 = boost::uniform_01<RealType>()(eng);
+ _r2 = boost::uniform_01<RealType>()(eng);
+ _cached_rho = sqrt(-result_type(2) * log(result_type(1)-_r2));
+ _valid = true;
+ } else {
+ _valid = false;
+ }
+ // Can we have a boost::mathconst please?
+ const result_type pi = result_type(3.14159265358979323846);
+
+ return _cached_rho * (_valid ?
+ cos(result_type(2)*pi*_r1) :
+ sin(result_type(2)*pi*_r1))
+ * _sigma + _mean;
+ }
+
+ /** Returns a normal variate with parameters specified by @c param. */
+ template<class URNG>
+ result_type operator()(URNG& urng, const param_type& parm)
+ {
+ return normal_distribution(parm)(urng);
+ }
+
+ /** Writes a @c normal_distribution to a @c std::ostream. */
+ BOOST_RANDOM_DETAIL_OSTREAM_OPERATOR(os, normal_distribution, nd)
+ {
+ os << nd._mean << " " << nd._sigma << " "
+ << nd._valid << " " << nd._cached_rho << " " << nd._r1;
+ return os;
+ }
+
+ /** Reads a @c normal_distribution from a @c std::istream. */
+ BOOST_RANDOM_DETAIL_ISTREAM_OPERATOR(is, normal_distribution, nd)
+ {
+ is >> std::ws >> nd._mean >> std::ws >> nd._sigma
+ >> std::ws >> nd._valid >> std::ws >> nd._cached_rho
+ >> std::ws >> nd._r1;
+ return is;
+ }
+
+ /**
+ * Returns true if the two instances of @c normal_distribution will
+ * return identical sequences of values given equal generators.
+ */
+ BOOST_RANDOM_DETAIL_EQUALITY_OPERATOR(normal_distribution, lhs, rhs)
+ {
+ return lhs._mean == rhs._mean && lhs._sigma == rhs._sigma
+ && lhs._valid == rhs._valid
+ && (!lhs._valid || (lhs._r1 == rhs._r1 && lhs._r2 == rhs._r2));
+ }
+
+ /**
+ * Returns true if the two instances of @c normal_distribution will
+ * return different sequences of values given equal generators.
+ */
+ BOOST_RANDOM_DETAIL_INEQUALITY_OPERATOR(normal_distribution)
+