/src/rt/isaac/rand.h
C Header | 56 lines | 24 code | 9 blank | 23 comment | 0 complexity | 028dc6b6831999740230b8a84500ba62 MD5 | raw file
1/* 2------------------------------------------------------------------------------ 3rand.h: definitions for a random number generator 4By Bob Jenkins, 1996, Public Domain 5MODIFIED: 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 */ 23struct randctx 24{ 25 ub4 randcnt; 26 ub4 randrsl[RANDSIZ]; 27 ub4 randmem[RANDSIZ]; 28 ub4 randa; 29 ub4 randb; 30 ub4 randc; 31}; 32typedef struct randctx randctx; 33 34/* 35------------------------------------------------------------------------------ 36 If (flag==TRUE), then use the contents of randrsl[0..RANDSIZ-1] as the seed. 37------------------------------------------------------------------------------ 38*/ 39void randinit(randctx *r, word flag); 40 41void isaac(randctx *r); 42 43 44/* 45------------------------------------------------------------------------------ 46 Call isaac_rand(/o_ randctx *r _o/) to retrieve a single 32-bit random value 47------------------------------------------------------------------------------ 48*/ 49#define isaac_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