* No longer use the OS's PRNG, we use our own (based on a Dr Dobbs article

from November 1985, page 91).
This commit is contained in:
Sam Hocevar
2007-01-03 21:20:22 +00:00
committed by sam
parent abb31b42e9
commit a786231fb2
+11 -5
View File
@@ -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;
}