diff -r 88efddb08ce6 src/core/random-variable.cc --- a/src/core/random-variable.cc Fri Mar 20 12:28:21 2009 -0400 +++ b/src/core/random-variable.cc Tue May 26 16:58:15 2009 +0200 @@ -1291,6 +1291,70 @@ : RandomVariable (TriangularVariableImpl (s,l,mean)) {} +//----------------------------------------------------------------------------- +//----------------------------------------------------------------------------- +// ZipfVariableImpl +class ZipfVariableImpl : public RandomVariableBase { +public: + /** + * \param n the number of possible items + * \param alpha the alpha parameter + */ + ZipfVariableImpl (long n, double alpha); + + /** + * \return A random value from this distribution + */ + virtual double GetValue (); + virtual RandomVariableBase* Copy(void) const; + +private: + long m_n; + double m_alpha; + double m_c; //the normalization constant +}; + + +RandomVariableBase* ZipfVariableImpl::Copy () const +{ + return new ZipfVariableImpl (m_n, m_alpha); +} + +ZipfVariableImpl::ZipfVariableImpl (long n, double alpha) + :m_n(n), m_alpha(alpha), m_c(0) +{ + //calculate the normalization constant c + for(int i=1;i<=n;i++) + m_c+=(1.0/pow((double)i,alpha)); + m_c=1.0/m_c; +} + +double +ZipfVariableImpl::GetValue () +{ + if(!m_generator) + { + m_generator = new RngStream(); + } + + double u = m_generator->RandU01(); + double sum_prob=0,zipf_value; + for(int i=1;i<=m_n;i++) + { + sum_prob+=m_c/pow((double)i,m_alpha); + if(sum_prob>u) + { + zipf_value=i; + break; + } + } + return zipf_value; +} + +ZipfVariable::ZipfVariable (long n, double alpha) + : RandomVariable (ZipfVariableImpl (n, alpha)) +{} + std::ostream &operator << (std::ostream &os, const RandomVariable &var) { diff -r 88efddb08ce6 src/core/random-variable.h --- a/src/core/random-variable.h Fri Mar 20 12:28:21 2009 -0400 +++ b/src/core/random-variable.h Tue May 26 16:58:15 2009 +0200 @@ -594,6 +594,21 @@ }; /** + * \brief Zipf Distributed random var + * \ingroup randomvariable + * + */ +class ZipfVariable : public RandomVariable +{ +public: + /** + * \param n the number of possible items + * \param alpha the alpha parameter + */ + ZipfVariable (long n, double alpha); +}; + +/** * \brief Triangularly Distributed random var * \ingroup randomvariable *