Changeset 1547


Ignore:
Timestamp:
Jan 3, 2007, 10:20:22 PM (14 years ago)
Author:
Sam Hocevar
Message:
  • No longer use the OS's PRNG, we use our own (based on a Dr Dobbs article from November 1985, page 91).
File:
1 edited

Legend:

Unmodified
Added
Removed
  • zzuf/trunk/src/random.c

    r1523 r1547  
    2828#include "random.h"
    2929
     30static unsigned long ctx = 1;
     31
    3032void _zz_srand(uint32_t seed)
    3133{
    32     srand(seed ^ 0x12345678);
     34    ctx = (seed ^ 0x12345678);
    3335}
    3436
    3537uint32_t _zz_rand(uint32_t max)
    3638{
    37     if(max <= RAND_MAX)
    38         return rand() % max;
     39    /* Could be better, but do we care? */
     40    long hi, lo, x;
    3941
    40     /* Could be better, but do we care? */
    41     return (uint32_t)((max * 1.0) * (rand() / (RAND_MAX + 1.0)));
     42    hi = ctx / 12773L;
     43    lo = ctx % 12773L;
     44    x = 16807L * lo - 2836L * hi;
     45    if(x <= 0)
     46        x += 0x7fffffffL;
     47    return (ctx = x) % (unsigned long)max;
    4248}
    4349
Note: See TracChangeset for help on using the changeset viewer.