From 600aa2ec161ce3260367faece5ab4f9798d55471 Mon Sep 17 00:00:00 2001 From: Raj Bhattacharjea Date: Wed, 18 Jul 2007 17:43:45 -0400 Subject: [PATCH] Added Triangularly distributed random variables --- src/core/random-variable.cc | 37 +++++++++++++++++++++++++++++++ src/core/random-variable.h | 44 +++++++++++++++++++++++++++++++++++++ 2 files changed, 81 insertions(+) diff --git a/src/core/random-variable.cc b/src/core/random-variable.cc index 824778c29..f93404fda 100644 --- a/src/core/random-variable.cc +++ b/src/core/random-variable.cc @@ -684,6 +684,43 @@ double LogNormalVariable::GetSingleValue (double mu, double sigma) return z; } +//----------------------------------------------------------------------------- +//----------------------------------------------------------------------------- +// TriangularVariable methods +TriangularVariable::TriangularVariable() + : m_min(0), m_max(1), m_mode(0.5) { } + +TriangularVariable::TriangularVariable(double s, double l, double mean) + : m_min(s), m_max(l), m_mode(3.0*mean-s-l) { } + +TriangularVariable::TriangularVariable(const TriangularVariable& c) + : RandomVariable(c), m_min(c.m_min), m_max(c.m_max), m_mode(c.m_mode) { } + +double TriangularVariable::GetValue() +{ + double u = m_generator->RandU01(); + if(u <= (m_mode - m_min) / (m_max - m_min) ) + return m_min + sqrt(u * (m_max - m_min) * (m_mode - m_min) ); + else + return m_max - sqrt( (1-u) * (m_max - m_min) * (m_max - m_mode) ); +} + +RandomVariable* TriangularVariable::Copy() const +{ + return new TriangularVariable(*this); +} + +double TriangularVariable::GetSingleValue(double s, double l, double mean) +{ + double mode = 3.0*mean-s-l; + double u = m_static_generator->RandU01(); + if(u <= (mode - s) / (l - s) ) + return s + sqrt(u * (l - s) * (mode - s) ); + else + return l - sqrt( (1-u) * (l - s) * (l - mode) ); +} + + }//namespace ns3 diff --git a/src/core/random-variable.h b/src/core/random-variable.h index 1007e878d..7759ea157 100644 --- a/src/core/random-variable.h +++ b/src/core/random-variable.h @@ -737,6 +737,50 @@ private: double m_sigma; }; +/** + * \brief Triangularly Distributed random var + * \ingroup randomvariable + * + * This distribution is a triangular distribution. The probablility density + * is in the shape of a triangle. + */ +class TriangularVariable : public RandomVariable { +public: + /** + * Creates a triangle distribution random number generator in the + * range [0.0 .. 1.0), with mean of 0.5 + */ + TriangularVariable(); + + /** + * Creates a triangle distribution random number generator with the specified + * range + * \param s Low end of the range + * \param l High end of the range + * \param mean mean of the distribution + */ + TriangularVariable(double s, double l, double mean); + + TriangularVariable(const TriangularVariable& c); + + /** + * \return A value from this distribution + */ + virtual double GetValue(); + virtual RandomVariable* Copy() const; +public: + /** + * \param s Low end of the range + * \param l High end of the range + * \return A triangularly distributed random number between s and l + */ + static double GetSingleValue(double s, double l, double mean); +private: + double m_min; + double m_max; + double m_mode; //easier to work with the mode internally instead of the mean + //they are related by the simple: mean = (min+max+mode)/3 +}; }//namespace ns3 #endif