分类:
2010-09-09 16:25:49
#include
#include
#include
#include
#define ALPHA_VALUE 0.73
int zipf_variate(int N, double alpha = ALPHA_VALUE)
{
double sigma = 0.0;
double sum = 0.0;
int i;
double r = (double)rand() / (double)RAND_MAX * 1.0;
for(i = 0; i < N; i++)
{
sigma += exp(-alpha*log(i+1));
}
for(i = 0; i < N; i++)
{
sum += exp(-alpha*log(i+1));
if (sum / sigma >= r)
return i;
}
return i;
}