INET Framework for OMNeT++/OMNEST
inet::httptools::rdZipf Class Reference

Zipf distribution random object. More...

#include <HttpRandom.h>

Inheritance diagram for inet::httptools::rdZipf:
inet::httptools::rdObject

Public Member Functions

 rdZipf (int n, double alpha, bool baseZero=false)
 
 rdZipf (cXMLAttributeMap attributes)
 
virtual double draw () override
 
virtual std::string toString () override
 
void setN (int n)
 
int getN ()
 
void setAlpha (double alpha)
 
double getAlpha ()
 
- Public Member Functions inherited from inet::httptools::rdObject
virtual ~rdObject ()
 
DISTR_TYPE getType ()
 
std::string typeStr ()
 

Protected Attributes

double m_alpha = NaN
 
int m_number = 0
 
double m_c = NaN
 
bool m_baseZero = false
 
- Protected Attributes inherited from inet::httptools::rdObject
DISTR_TYPE m_type = dt_normal
 

Private Member Functions

void __initialize (int n, double alpha, bool baseZero)
 
void __setup_c ()
 

Additional Inherited Members

- Protected Member Functions inherited from inet::httptools::rdObject
bool _hasKey (cXMLAttributeMap attributes, std::string key)
 

Detailed Description

Zipf distribution random object.

Returns a random value from a zipf distribution (1/n^a), where a is the constant alpha and n is a order of popularity. See more details on http://en.wikipedia.org/wiki/Zipf.

Constructor & Destructor Documentation

inet::httptools::rdZipf::rdZipf ( int  n,
double  alpha,
bool  baseZero = false 
)
274 {
275  m_type = dt_zipf;
276  __initialize(n, alpha, baseZero);
277 }
void __initialize(int n, double alpha, bool baseZero)
Definition: HttpRandom.cc:305
Definition: HttpRandom.h:33
DISTR_TYPE m_type
Definition: HttpRandom.h:49
const value< double, units::unit > alpha(7.2973525376e-3)
inet::httptools::rdZipf::rdZipf ( cXMLAttributeMap  attributes)
243 {
244  m_type = dt_zipf;
245 
246  if (!_hasKey(attributes, "n"))
247  throw "Undefined parameter for zipf distribution. n must be defined for an exponential distribution";
248  if (!_hasKey(attributes, "alpha"))
249  throw "Undefined parameter for zipf distribution. alpha must be defined for an exponential distribution";
250 
251  int n;
252  double alpha;
253  bool baseZero = false;
254 
255  if (_hasKey(attributes, "zeroBased"))
256  baseZero = strcmp(attributes["zeroBased"].c_str(), "true") == 0;
257 
258  try {
259  n = atoi(attributes["n"].c_str());
260  }
261  catch (...) {
262  n = 1;
263  }
264  try {
265  alpha = atof(attributes["alpha"].c_str());
266  }
267  catch (...) {
268  alpha = 1.0;
269  }
270  __initialize(n, alpha, baseZero);
271 }
void __initialize(int n, double alpha, bool baseZero)
Definition: HttpRandom.cc:305
Definition: HttpRandom.h:33
DISTR_TYPE m_type
Definition: HttpRandom.h:49
bool _hasKey(cXMLAttributeMap attributes, std::string key)
Definition: HttpRandom.h:52
const value< double, units::unit > alpha(7.2973525376e-3)

Member Function Documentation

void inet::httptools::rdZipf::__initialize ( int  n,
double  alpha,
bool  baseZero 
)
private
306 {
307  m_number = n;
308  m_alpha = alpha;
309  m_baseZero = baseZero;
310  __setup_c();
311 }
bool m_baseZero
Definition: HttpRandom.h:254
void __setup_c()
Definition: HttpRandom.cc:313
double m_alpha
Definition: HttpRandom.h:251
int m_number
Definition: HttpRandom.h:252
const value< double, units::unit > alpha(7.2973525376e-3)
void inet::httptools::rdZipf::__setup_c ( )
private
314 {
315  m_c = 0.0;
316  for (int i = 1; i <= m_number; i++)
317  m_c += (1.0 / pow((double)i, m_alpha));
318  m_c = 1.0 / m_c;
319 }
double m_c
Definition: HttpRandom.h:253
double m_alpha
Definition: HttpRandom.h:251
int m_number
Definition: HttpRandom.h:252
double inet::httptools::rdZipf::draw ( )
overridevirtual

Implements inet::httptools::rdObject.

280 {
281  double sum_prob = 0;
282  double z = RNGCONTEXT uniform(0.0001, 0.9999);
283  int i;
284  for (i = 1; i <= m_number; i++) {
285  sum_prob += m_c / pow((double)i, m_alpha);
286  if (sum_prob >= z)
287  break;
288  }
289  if (m_baseZero)
290  return i - 1;
291  else
292  return i;
293 }
bool m_baseZero
Definition: HttpRandom.h:254
double m_c
Definition: HttpRandom.h:253
#define RNGCONTEXT
Definition: INETDefs.h:85
double m_alpha
Definition: HttpRandom.h:251
int m_number
Definition: HttpRandom.h:252
double inet::httptools::rdZipf::getAlpha ( )
inline
287 { return m_alpha; }
double m_alpha
Definition: HttpRandom.h:251
int inet::httptools::rdZipf::getN ( )
inline
285 { return m_number; }
int m_number
Definition: HttpRandom.h:252
void inet::httptools::rdZipf::setAlpha ( double  alpha)
inline
286 { m_alpha = alpha; __setup_c(); }
void __setup_c()
Definition: HttpRandom.cc:313
double m_alpha
Definition: HttpRandom.h:251
const value< double, units::unit > alpha(7.2973525376e-3)
void inet::httptools::rdZipf::setN ( int  n)
inline
284 { m_number = n; __setup_c(); }
void __setup_c()
Definition: HttpRandom.cc:313
int m_number
Definition: HttpRandom.h:252
std::string inet::httptools::rdZipf::toString ( )
overridevirtual

Reimplemented from inet::httptools::rdObject.

296 {
297  std::ostringstream str;
298  str << "Zipf probability distribution. n=" << m_number << ", alpha=" << m_alpha;
299  if (m_baseZero)
300  str << " Zero-based";
301  str << endl;
302  return str.str();
303 }
bool m_baseZero
Definition: HttpRandom.h:254
double m_alpha
Definition: HttpRandom.h:251
int m_number
Definition: HttpRandom.h:252

Member Data Documentation

double inet::httptools::rdZipf::m_alpha = NaN
protected
bool inet::httptools::rdZipf::m_baseZero = false
protected
double inet::httptools::rdZipf::m_c = NaN
protected
int inet::httptools::rdZipf::m_number = 0
protected

The documentation for this class was generated from the following files: