diff --git a/src/random.c b/src/random.c index cbb36e1..5cd2996 100644 --- a/src/random.c +++ b/src/random.c @@ -27,17 +27,23 @@ #include "random.h" +static unsigned long ctx = 1; + void _zz_srand(uint32_t seed) { - srand(seed ^ 0x12345678); + ctx = (seed ^ 0x12345678); } uint32_t _zz_rand(uint32_t max) { - if(max <= RAND_MAX) - return rand() % max; - /* Could be better, but do we care? */ - return (uint32_t)((max * 1.0) * (rand() / (RAND_MAX + 1.0))); + long hi, lo, x; + + hi = ctx / 12773L; + lo = ctx % 12773L; + x = 16807L * lo - 2836L * hi; + if(x <= 0) + x += 0x7fffffffL; + return (ctx = x) % (unsigned long)max; }