11 #include "../minmax.h"
14 #define ZIPF_MAX_GEN 10000000
16 static void zipf_update(struct zipf_state *zs)
22 * It can become very costly to generate long sequences. Just cap it at
23 * 10M max, that should be doable in 1-2s on even slow machines.
24 * Precision will take a slight hit, but nothing major.
26 to_gen = min(zs->nranges, ZIPF_MAX_GEN);
28 for (i = 0; i < to_gen; i++)
29 zs->zetan += pow(1.0 / (double) (i + 1), zs->theta);
32 static void shared_rand_init(struct zipf_state *zs, unsigned long nranges,
35 memset(zs, 0, sizeof(*zs));
36 zs->nranges = nranges;
38 init_rand_seed(&zs->rand, seed);
39 zs->rand_off = __rand(&zs->rand);
42 void zipf_init(struct zipf_state *zs, unsigned long nranges, double theta,
45 shared_rand_init(zs, nranges, seed);
48 zs->zeta2 = pow(1.0, zs->theta) + pow(0.5, zs->theta);
53 unsigned long long zipf_next(struct zipf_state *zs)
55 double alpha, eta, rand_uni, rand_z;
56 unsigned long long n = zs->nranges;
57 unsigned long long val;
59 alpha = 1.0 / (1.0 - zs->theta);
60 eta = (1.0 - pow(2.0 / n, 1.0 - zs->theta)) / (1.0 - zs->zeta2 / zs->zetan);
62 rand_uni = (double) __rand(&zs->rand) / (double) FRAND_MAX;
63 rand_z = rand_uni * zs->zetan;
67 else if (rand_z < (1.0 + pow(0.5, zs->theta)))
70 val = 1 + (unsigned long long)(n * pow(eta*rand_uni - eta + 1.0, alpha));
72 return (__hash_long(val - 1) + zs->rand_off) % zs->nranges;
75 void pareto_init(struct zipf_state *zs, unsigned long nranges, double h,
78 shared_rand_init(zs, nranges, seed);
79 zs->pareto_pow = log(h) / log(1.0 - h);
82 unsigned long long pareto_next(struct zipf_state *zs)
84 double rand = (double) __rand(&zs->rand) / (double) FRAND_MAX;
85 unsigned long long n = zs->nranges - 1;
87 return (__hash_long(n * pow(rand, zs->pareto_pow)) + zs->rand_off) % zs->nranges;