1 /* 2 ------------------------------------------------------------------------------ 3 rand.h: definitions for a random number generator 4 By Bob Jenkins, 1996, Public Domain 5 MODIFIED: 6 960327: Creation (addition of randinit, really) 7 970719: use context, not global variables, for internal state 8 980324: renamed seed to flag 9 980605: recommend RANDSIZL=4 for noncryptography. 10 010626: note this is public domain 11 ------------------------------------------------------------------------------ 12 */ 13 #ifndef STANDARD 14 #include "standard.h" 15 #endif 16 17 #ifndef RAND 18 #define RAND 19 #define RANDSIZL (8) /* I recommend 8 for crypto, 4 for simulations */ 20 #define RANDSIZ (1<<RANDSIZL) 21 22 /* context of random number generator */ 23 struct randctx 24 { 25 ub4 randcnt; 26 ub4 randrsl[RANDSIZ]; 27 ub4 randmem[RANDSIZ]; 28 ub4 randa; 29 ub4 randb; 30 ub4 randc; 31 }; 32 typedef struct randctx randctx; 33 34 /* 35 ------------------------------------------------------------------------------ 36 If (flag==TRUE), then use the contents of randrsl[0..RANDSIZ-1] as the seed. 37 ------------------------------------------------------------------------------ 38 */ 39 void randinit(randctx *r, word flag); 40 41 void isaac(randctx *r); 42 43 44 /* 45 ------------------------------------------------------------------------------ 46 Call rand(/o_ randctx *r _o/) to retrieve a single 32-bit random value 47 ------------------------------------------------------------------------------ 48 */ 49 #define rand(r) \ 50 (!(r)->randcnt-- ? \ 51 (isaac(r), (r)->randcnt=RANDSIZ-1, (r)->randrsl[(r)->randcnt]) : \ 52 (r)->randrsl[(r)->randcnt]) 53 54 #endif /* RAND */ 55 56 57