View | Details | Raw Unified | Return to bug 578
Collapse All | Expand All

(-)a/src/core/random-variable.cc (+64 lines)
 Lines 1291-1296    Link Here 
1291
  : RandomVariable (TriangularVariableImpl (s,l,mean))
1291
  : RandomVariable (TriangularVariableImpl (s,l,mean))
1292
{}
1292
{}
1293
1293
1294
//-----------------------------------------------------------------------------
1295
//-----------------------------------------------------------------------------
1296
// ZipfVariableImpl
1297
class ZipfVariableImpl : public RandomVariableBase { 
1298
public:
1299
  /**
1300
   * \param n the number of possible items
1301
   * \param alpha the alpha parameter
1302
   */
1303
  ZipfVariableImpl (long n, double alpha);
1304
1305
  /**
1306
   * \return A random value from this distribution
1307
   */
1308
  virtual double GetValue ();
1309
  virtual RandomVariableBase* Copy(void) const;
1310
1311
private:
1312
  long m_n;
1313
  double m_alpha;
1314
  double m_c; //the normalization constant
1315
};
1316
1317
1318
RandomVariableBase* ZipfVariableImpl::Copy () const
1319
{
1320
  return new ZipfVariableImpl (m_n, m_alpha);
1321
}
1322
1323
ZipfVariableImpl::ZipfVariableImpl (long n, double alpha)
1324
    :m_n(n), m_alpha(alpha), m_c(0)
1325
{
1326
  //calculate the normalization constant c
1327
  for(int i=1;i<=n;i++)
1328
    m_c+=(1.0/pow((double)i,alpha));
1329
  m_c=1.0/m_c;
1330
}
1331
1332
double
1333
ZipfVariableImpl::GetValue ()
1334
{
1335
  if(!m_generator)
1336
  {
1337
    m_generator = new RngStream();
1338
  }
1339
1340
  double u = m_generator->RandU01();
1341
  double sum_prob=0,zipf_value;
1342
  for(int i=1;i<=m_n;i++)
1343
  {
1344
    sum_prob+=m_c/pow((double)i,m_alpha);
1345
    if(sum_prob>u)
1346
    {
1347
      zipf_value=i;
1348
      break;
1349
    }
1350
  }
1351
  return zipf_value;
1352
}
1353
1354
ZipfVariable::ZipfVariable (long n, double alpha)
1355
  : RandomVariable (ZipfVariableImpl (n, alpha))
1356
{}
1357
1294
1358
1295
std::ostream &operator << (std::ostream &os, const RandomVariable &var)
1359
std::ostream &operator << (std::ostream &os, const RandomVariable &var)
1296
{
1360
{
(-)a/src/core/random-variable.h (+15 lines)
 Lines 594-599    Link Here 
594
};
594
};
595
595
596
/**
596
/**
597
 * \brief Zipf Distributed random var
598
 * \ingroup randomvariable
599
 *
600
 */
601
class ZipfVariable : public RandomVariable 
602
{
603
public:
604
  /**
605
   * \param n the number of possible items
606
   * \param alpha the alpha parameter
607
   */
608
  ZipfVariable (long n, double alpha);
609
};
610
611
/**
597
 * \brief Triangularly Distributed random var
612
 * \brief Triangularly Distributed random var
598
 * \ingroup randomvariable
613
 * \ingroup randomvariable
599
 * 
614
 * 

Return to bug 578