blob: 73f6e3e44f40dea816c284ca83fe1465ebfdd192 [file] [log] [blame]
Dees_Troy83bd4832013-05-04 12:39:56 +00001/*
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)
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 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