]> jfr.im git - irc/quakenet/newserv.git/blob - lib/prng.h
A4STATS: remove E style escapes and switch to createtable for indices
[irc/quakenet/newserv.git] / lib / prng.h
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 # define STANDARD
15 # ifndef STDIO
16 # include <stdio.h>
17 # define STDIO
18 # endif
19 # ifndef STDDEF
20 # include <stddef.h>
21 # define STDDEF
22 # endif
23 typedef unsigned long long ub8;
24 #define UB8MAXVAL 0xffffffffffffffffLL
25 #define UB8BITS 64
26 typedef signed long long sb8;
27 #define SB8MAXVAL 0x7fffffffffffffffLL
28 typedef unsigned long int ub4; /* unsigned 4-byte quantities */
29 #define UB4MAXVAL 0xffffffff
30 typedef signed long int sb4;
31 #define UB4BITS 32
32 #define SB4MAXVAL 0x7fffffff
33 typedef unsigned short int ub2;
34 #define UB2MAXVAL 0xffff
35 #define UB2BITS 16
36 typedef signed short int sb2;
37 #define SB2MAXVAL 0x7fff
38 typedef unsigned char ub1;
39 #define UB1MAXVAL 0xff
40 #define UB1BITS 8
41 typedef signed char sb1; /* signed 1-byte quantities */
42 #define SB1MAXVAL 0x7f
43 typedef int word; /* fastest type available */
44
45 #define bis(target,mask) ((target) |= (mask))
46 #define bic(target,mask) ((target) &= ~(mask))
47 #define bit(target,mask) ((target) & (mask))
48 #ifndef min
49 # define min(a,b) (((a)<(b)) ? (a) : (b))
50 #endif /* min */
51 #ifndef max
52 # define max(a,b) (((a)<(b)) ? (b) : (a))
53 #endif /* max */
54 #ifndef align
55 # define align(a) (((ub4)a+(sizeof(void *)-1))&(~(sizeof(void *)-1)))
56 #endif /* align */
57 #ifndef abs
58 # define abs(a) (((a)>0) ? (a) : -(a))
59 #endif
60 #define TRUE 1
61 #define FALSE 0
62 #define SUCCESS 0 /* 1 on VAX */
63
64 #endif /* STANDARD */
65
66 #ifndef PRNG
67 #define PRNG
68 #define RANDSIZL (8) /* I recommend 8 for crypto, 4 for simulations */
69 #define RANDSIZ (1<<RANDSIZL)
70
71 /* context of random number generator */
72 struct prngctx
73 {
74 ub4 randcnt;
75 ub4 randrsl[RANDSIZ];
76 ub4 randmem[RANDSIZ];
77 ub4 randa;
78 ub4 randb;
79 ub4 randc;
80 };
81 typedef struct prngctx prngctx;
82
83 /*
84 ------------------------------------------------------------------------------
85 If (flag==TRUE), then use the contents of randrsl[0..RANDSIZ-1] as the seed.
86 ------------------------------------------------------------------------------
87 */
88 void prnginit(/*_ prngctx *r, word flag _*/);
89
90 void isaac(/*_ prngctx *r _*/);
91
92
93 /*
94 ------------------------------------------------------------------------------
95 Call rand(/o_ prngctx *r _o/) to retrieve a single 32-bit random value
96 ------------------------------------------------------------------------------
97 */
98 #define prng(r) \
99 (!(r)->randcnt-- ? \
100 (isaac(r), (r)->randcnt=RANDSIZ-1, (r)->randrsl[(r)->randcnt]) : \
101 (r)->randrsl[(r)->randcnt])
102
103 #endif /* RAND */
104
105