20 void random_lib_lp_init(
void)
24 struct lib_ctx *ctx = lib_ctx_get();
25 random_init(ctx->rng_s, lid, seed);
31 struct lib_ctx *ctx = lib_ctx_get();
32 uint64_t u_val = random_u64(ctx->rng_s);
41 uint64_t exp = 1023 - lzs;
44 memcpy(&ret, &u_val,
sizeof(
double));
48 uint64_t RandomU64(
void)
50 struct lib_ctx *ctx = lib_ctx_get();
51 return random_u64(ctx->rng_s);
66 return -mean * log(1 - Random());
76 struct lib_ctx *ctx = lib_ctx_get();
77 if (isnan(ctx->unif)) {
80 v1 = 2.0 * Random() - 1.0;
81 v2 = 2.0 * Random() - 1.0;
82 rsq = v1 * v1 + v2 * v2;
83 }
while (rsq >= 1.0 || rsq == 0);
85 double fac = sqrt(-2.0 * log(rsq) / rsq);
93 double ret = ctx->unif;
99 int RandomRange(
int min,
int max)
101 return (
int)floor(Random() * (max - min + 1)) + min;
104 int RandomRangeNonUniform(
int x,
int min,
int max)
106 return (((RandomRange(0, x) | RandomRange(min, max))) %
107 (max - min + 1)) + min;
122 "value >= 1. Defaulting to 1...");
136 double v1, v2, e, y, s;
142 v2 = 2.0 * Random() - 1.0;
143 }
while (v1 * v1 + v2 * v2 > 1.0);
146 s = sqrt(2.0 * am + 1.0);
150 e = (1.0 + y * y) * exp(am * log(x / am) - s * y);
151 }
while (Random() > e);
164 return -log(1 - Random());
176 unsigned Zipf(
double skew,
unsigned limit)
178 double b = pow(2., skew - 1.);
181 x = floor(pow(Random(), -1. / skew - 1.));
182 t = pow(1. + 1. / x, skew - 1.);
183 }
while (x > limit || Random() * x * (t - 1.) * b > t * (b - 1.));