rand.h

00001 /*
00002 ------------------------------------------------------------------------------
00003 rand.h: definitions for a random number generator
00004 By Bob Jenkins, 1996, Public Domain
00005 MODIFIED:
00006   960327: Creation (addition of randinit, really)
00007   970719: use context, not global variables, for internal state
00008   980324: renamed seed to flag
00009   980605: recommend RANDSIZL=4 for noncryptography.
00010   010626: note this is public domain
00011 ------------------------------------------------------------------------------
00012 */
00013 #ifndef STANDARD
00014 #include "standard.h"
00015 #endif
00016 
00017 #ifndef RAND
00018 #define RAND
00019 #define RANDSIZL   (8)  /* I recommend 8 for crypto, 4 for simulations */
00020 #define RANDSIZ    (1<<RANDSIZL)
00021 
00022 /* context of random number generator */
00023 struct randctx
00024 {
00025   ub4 randcnt;
00026   ub4 randrsl[RANDSIZ];
00027   ub4 randmem[RANDSIZ];
00028   ub4 randa;
00029   ub4 randb;
00030   ub4 randc;
00031 };
00032 typedef  struct randctx  randctx;
00033 
00034 /*
00035 ------------------------------------------------------------------------------
00036  If (flag==TRUE), then use the contents of randrsl[0..RANDSIZ-1] as the seed.
00037 ------------------------------------------------------------------------------
00038 */
00039 void randinit(randctx *r, word flag);
00040 
00041 void isaac(randctx *r);
00042 
00043 
00044 /*
00045 ------------------------------------------------------------------------------
00046  Call rand(/o_ randctx *r _o/) to retrieve a single 32-bit random value
00047 ------------------------------------------------------------------------------
00048 */
00049 #define rand(r) \
00050    (!(r)->randcnt-- ? \
00051      (isaac(r), (r)->randcnt=RANDSIZ-1, (r)->randrsl[(r)->randcnt]) : \
00052      (r)->randrsl[(r)->randcnt])
00053 
00054 #endif  /* RAND */
00055 
00056