1 /* Random kit 1.3 */
2 
3 /*
4  * Copyright (c) 2003-2005, Jean-Sebastien Roy (js@jeannot.org)
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the
8  * "Software"), to deal in the Software without restriction, including
9  * without limitation the rights to use, copy, modify, merge, publish,
10  * distribute, sublicense, and/or sell copies of the Software, and to
11  * permit persons to whom the Software is furnished to do so, subject to
12  * the following conditions:
13  *
14  * The above copyright notice and this permission notice shall be included
15  * in all copies or substantial portions of the Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
20  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
21  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
22  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
23  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24  */
25 
26 /* @(#) $Jeannot: randomkit.h,v 1.24 2005/07/21 22:14:09 js Exp $ */
27 
28 /*
29  * Typical use:
30  *
31  * {
32  *  rk_state state;
33  *  unsigned long seed = 1, random_value;
34  *
35  *  rk_seed(seed, &state); // Initialize the RNG
36  *  ...
37  *  random_value = rk_random(&state); // Generate random values in [0..RK_MAX]
38  * }
39  *
40  * Instead of rk_seed, you can use rk_randomseed which will get a random seed
41  * from /dev/urandom (or the clock, if /dev/urandom is unavailable):
42  *
43  * {
44  *  rk_state state;
45  *  unsigned long random_value;
46  *
47  *  rk_randomseed(&state); // Initialize the RNG with a random seed
48  *  ...
49  *  random_value = rk_random(&state); // Generate random values in [0..RK_MAX]
50  * }
51  */
52 
53 /*
54  * Useful macro:
55  *  RK_DEV_RANDOM: the device used for random seeding.
56  *                 defaults to "/dev/urandom"
57  */
58 
59 #include <stddef.h>
60 
61 #ifndef _RANDOMKIT_
62 #define _RANDOMKIT_
63 
64 #define RK_STATE_LEN 624
65 
66 typedef struct rk_state_
67 {
68     unsigned long key[RK_STATE_LEN];
69     int pos;
70     int has_gauss; /* !=0: gauss contains a gaussian deviate */
71     double gauss;
72 
73     /* The rk_state structure has been extended to store the following
74      * information for the binomial generator. If the input values of n or p
75      * are different than nsave and psave, then the other parameters will be
76      * recomputed. RTK 2005-09-02 */
77 
78     int has_binomial; /* !=0: following parameters initialized for
79                               binomial */
80     double psave;
81     long nsave;
82     double r;
83     double q;
84     double fm;
85     long m;
86     double p1;
87     double xm;
88     double xl;
89     double xr;
90     double c;
91     double laml;
92     double lamr;
93     double p2;
94     double p3;
95     double p4;
96 
97 }
98 rk_state;
99 
100 typedef enum {
101     RK_NOERR = 0, /* no error */
102     RK_ENODEV = 1, /* no RK_DEV_RANDOM device */
103     RK_ERR_MAX = 2
104 } rk_error;
105 
106 /* error strings */
107 extern char *rk_strerror[RK_ERR_MAX];
108 
109 /* Maximum generated random value */
110 #define RK_MAX 0xFFFFFFFFUL
111 
112 #ifdef __cplusplus
113 extern "C" {
114 #endif
115 
116 /*
117  * Initialize the RNG state using the given seed.
118  */
119 extern void rk_seed(unsigned long seed, rk_state *state);
120 
121 /*
122  * Returns a random unsigned long between 0 and RK_MAX inclusive
123  */
124 extern unsigned long rk_random(rk_state *state);
125 
126 /*
127  * Returns a random long between 0 and LONG_MAX inclusive
128  */
129 extern long rk_long(rk_state *state);
130 
131 /*
132  * Returns a random unsigned long between 0 and ULONG_MAX inclusive
133  */
134 extern unsigned long rk_ulong(rk_state *state);
135 
136 /*
137  * Returns a random unsigned long between 0 and max inclusive.
138  */
139 extern unsigned long rk_interval(unsigned long max, rk_state *state);
140 
141 /*
142  * Returns a random double between 0.0 and 1.0, 1.0 excluded.
143  */
144 extern double rk_double(rk_state *state);
145 
146 #ifdef __cplusplus
147 }
148 #endif
149 
150 #endif /* _RANDOMKIT_ */
151