* 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:
+11
-5
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user