--- a/src/core/random-variable.cc Wed Jul 18 09:27:22 2007 -0500
+++ b/src/core/random-variable.cc Wed Jul 18 17:43:45 2007 -0400
@@ -684,6 +684,43 @@
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
--- a/src/core/random-variable.h Wed Jul 18 09:27:22 2007 -0500
+++ b/src/core/random-variable.h Wed Jul 18 17:43:45 2007 -0400
@@ -737,6 +737,50 @@
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