1 1.1 mrg /* 2 1.1 mrg * Copyright (c) 1983 Regents of the University of California. 3 1.1 mrg * All rights reserved. 4 1.1 mrg * 5 1.1 mrg * Redistribution and use in source and binary forms, with or without 6 1.1 mrg * modification, are permitted provided that the following conditions 7 1.1 mrg * are met: 8 1.1 mrg * 1. Redistributions of source code must retain the above copyright 9 1.1 mrg * notice, this list of conditions and the following disclaimer. 10 1.1 mrg * 2. Redistributions in binary form must reproduce the above copyright 11 1.1 mrg * notice, this list of conditions and the following disclaimer in the 12 1.1 mrg * documentation and/or other materials provided with the distribution. 13 1.1 mrg * 3. [rescinded 22 July 1999] 14 1.1 mrg * 4. Neither the name of the University nor the names of its contributors 15 1.1 mrg * may be used to endorse or promote products derived from this software 16 1.1 mrg * without specific prior written permission. 17 1.1 mrg * 18 1.1 mrg * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 19 1.1 mrg * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20 1.1 mrg * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21 1.1 mrg * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 22 1.1 mrg * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 23 1.1 mrg * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 24 1.1 mrg * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 25 1.1 mrg * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 26 1.1 mrg * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 27 1.1 mrg * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 28 1.1 mrg * SUCH DAMAGE. 29 1.1 mrg */ 30 1.1 mrg 31 1.1 mrg /* 32 1.1 mrg * This is derived from the Berkeley source: 33 1.1 mrg * @(#)random.c 5.5 (Berkeley) 7/6/88 34 1.1 mrg * It was reworked for the GNU C Library by Roland McGrath. 35 1.1 mrg */ 36 1.1 mrg 37 1.1 mrg /* 38 1.1 mrg 39 1.1 mrg @deftypefn Supplement {long int} random (void) 40 1.1 mrg @deftypefnx Supplement void srandom (unsigned int @var{seed}) 41 1.1.1.2 mrg @deftypefnx Supplement void* initstate (unsigned int @var{seed}, @ 42 1.1.1.2 mrg void *@var{arg_state}, unsigned long @var{n}) 43 1.1 mrg @deftypefnx Supplement void* setstate (void *@var{arg_state}) 44 1.1 mrg 45 1.1 mrg Random number functions. @code{random} returns a random number in the 46 1.1 mrg range 0 to @code{LONG_MAX}. @code{srandom} initializes the random 47 1.1 mrg number generator to some starting point determined by @var{seed} 48 1.1 mrg (else, the values returned by @code{random} are always the same for each 49 1.1 mrg run of the program). @code{initstate} and @code{setstate} allow fine-grained 50 1.1 mrg control over the state of the random number generator. 51 1.1 mrg 52 1.1 mrg @end deftypefn 53 1.1 mrg 54 1.1 mrg */ 55 1.1 mrg 56 1.1 mrg #include <errno.h> 57 1.1 mrg 58 1.1 mrg #if 0 59 1.1 mrg 60 1.1 mrg #include <ansidecl.h> 61 1.1 mrg #include <limits.h> 62 1.1 mrg #include <stddef.h> 63 1.1 mrg #include <stdlib.h> 64 1.1 mrg 65 1.1 mrg #else 66 1.1 mrg 67 1.1 mrg #define ULONG_MAX ((unsigned long)(~0L)) /* 0xFFFFFFFF for 32-bits */ 68 1.1 mrg #define LONG_MAX ((long)(ULONG_MAX >> 1)) /* 0x7FFFFFFF for 32-bits*/ 69 1.1 mrg 70 1.1 mrg #ifdef __STDC__ 71 1.1 mrg # ifndef NULL 72 1.1 mrg # define NULL (void *) 0 73 1.1 mrg # endif 74 1.1 mrg #else 75 1.1 mrg # ifndef NULL 76 1.1 mrg # define NULL (void *) 0 77 1.1 mrg # endif 78 1.1 mrg #endif 79 1.1 mrg 80 1.1 mrg #endif 81 1.1 mrg 82 1.1 mrg long int random (void); 83 1.1 mrg 84 1.1 mrg /* An improved random number generation package. In addition to the standard 85 1.1 mrg rand()/srand() like interface, this package also has a special state info 86 1.1 mrg interface. The initstate() routine is called with a seed, an array of 87 1.1 mrg bytes, and a count of how many bytes are being passed in; this array is 88 1.1 mrg then initialized to contain information for random number generation with 89 1.1 mrg that much state information. Good sizes for the amount of state 90 1.1 mrg information are 32, 64, 128, and 256 bytes. The state can be switched by 91 1.1 mrg calling the setstate() function with the same array as was initiallized 92 1.1 mrg with initstate(). By default, the package runs with 128 bytes of state 93 1.1 mrg information and generates far better random numbers than a linear 94 1.1 mrg congruential generator. If the amount of state information is less than 95 1.1 mrg 32 bytes, a simple linear congruential R.N.G. is used. Internally, the 96 1.1 mrg state information is treated as an array of longs; the zeroeth element of 97 1.1 mrg the array is the type of R.N.G. being used (small integer); the remainder 98 1.1 mrg of the array is the state information for the R.N.G. Thus, 32 bytes of 99 1.1 mrg state information will give 7 longs worth of state information, which will 100 1.1 mrg allow a degree seven polynomial. (Note: The zeroeth word of state 101 1.1 mrg information also has some other information stored in it; see setstate 102 1.1 mrg for details). The random number generation technique is a linear feedback 103 1.1 mrg shift register approach, employing trinomials (since there are fewer terms 104 1.1 mrg to sum up that way). In this approach, the least significant bit of all 105 1.1 mrg the numbers in the state table will act as a linear feedback shift register, 106 1.1 mrg and will have period 2^deg - 1 (where deg is the degree of the polynomial 107 1.1 mrg being used, assuming that the polynomial is irreducible and primitive). 108 1.1 mrg The higher order bits will have longer periods, since their values are 109 1.1 mrg also influenced by pseudo-random carries out of the lower bits. The 110 1.1 mrg total period of the generator is approximately deg*(2**deg - 1); thus 111 1.1 mrg doubling the amount of state information has a vast influence on the 112 1.1 mrg period of the generator. Note: The deg*(2**deg - 1) is an approximation 113 1.1 mrg only good for large deg, when the period of the shift register is the 114 1.1 mrg dominant factor. With deg equal to seven, the period is actually much 115 1.1 mrg longer than the 7*(2**7 - 1) predicted by this formula. */ 116 1.1 mrg 117 1.1 mrg 118 1.1 mrg 119 1.1 mrg /* For each of the currently supported random number generators, we have a 120 1.1 mrg break value on the amount of state information (you need at least thi 121 1.1 mrg bytes of state info to support this random number generator), a degree for 122 1.1 mrg the polynomial (actually a trinomial) that the R.N.G. is based on, and 123 1.1 mrg separation between the two lower order coefficients of the trinomial. */ 124 1.1 mrg 125 1.1 mrg /* Linear congruential. */ 126 1.1 mrg #define TYPE_0 0 127 1.1 mrg #define BREAK_0 8 128 1.1 mrg #define DEG_0 0 129 1.1 mrg #define SEP_0 0 130 1.1 mrg 131 1.1 mrg /* x**7 + x**3 + 1. */ 132 1.1 mrg #define TYPE_1 1 133 1.1 mrg #define BREAK_1 32 134 1.1 mrg #define DEG_1 7 135 1.1 mrg #define SEP_1 3 136 1.1 mrg 137 1.1 mrg /* x**15 + x + 1. */ 138 1.1 mrg #define TYPE_2 2 139 1.1 mrg #define BREAK_2 64 140 1.1 mrg #define DEG_2 15 141 1.1 mrg #define SEP_2 1 142 1.1 mrg 143 1.1 mrg /* x**31 + x**3 + 1. */ 144 1.1 mrg #define TYPE_3 3 145 1.1 mrg #define BREAK_3 128 146 1.1 mrg #define DEG_3 31 147 1.1 mrg #define SEP_3 3 148 1.1 mrg 149 1.1 mrg /* x**63 + x + 1. */ 150 1.1 mrg #define TYPE_4 4 151 1.1 mrg #define BREAK_4 256 152 1.1 mrg #define DEG_4 63 153 1.1 mrg #define SEP_4 1 154 1.1 mrg 155 1.1 mrg 156 1.1 mrg /* Array versions of the above information to make code run faster. 157 1.1 mrg Relies on fact that TYPE_i == i. */ 158 1.1 mrg 159 1.1 mrg #define MAX_TYPES 5 /* Max number of types above. */ 160 1.1 mrg 161 1.1 mrg static int degrees[MAX_TYPES] = { DEG_0, DEG_1, DEG_2, DEG_3, DEG_4 }; 162 1.1 mrg static int seps[MAX_TYPES] = { SEP_0, SEP_1, SEP_2, SEP_3, SEP_4 }; 163 1.1 mrg 164 1.1 mrg 165 1.1 mrg 166 1.1 mrg /* Initially, everything is set up as if from: 167 1.1 mrg initstate(1, randtbl, 128); 168 1.1 mrg Note that this initialization takes advantage of the fact that srandom 169 1.1 mrg advances the front and rear pointers 10*rand_deg times, and hence the 170 1.1 mrg rear pointer which starts at 0 will also end up at zero; thus the zeroeth 171 1.1 mrg element of the state information, which contains info about the current 172 1.1 mrg position of the rear pointer is just 173 1.1 mrg (MAX_TYPES * (rptr - state)) + TYPE_3 == TYPE_3. */ 174 1.1 mrg 175 1.1 mrg static long int randtbl[DEG_3 + 1] = 176 1.1 mrg { TYPE_3, 177 1.1 mrg 0x9a319039, 0x32d9c024, 0x9b663182, 0x5da1f342, 178 1.1 mrg 0xde3b81e0, 0xdf0a6fb5, 0xf103bc02, 0x48f340fb, 179 1.1 mrg 0x7449e56b, 0xbeb1dbb0, 0xab5c5918, 0x946554fd, 180 1.1 mrg 0x8c2e680f, 0xeb3d799f, 0xb11ee0b7, 0x2d436b86, 181 1.1 mrg 0xda672e2a, 0x1588ca88, 0xe369735d, 0x904f35f7, 182 1.1 mrg 0xd7158fd6, 0x6fa6f051, 0x616e6b96, 0xac94efdc, 183 1.1 mrg 0x36413f93, 0xc622c298, 0xf5a42ab8, 0x8a88d77b, 184 1.1 mrg 0xf5ad9d0e, 0x8999220b, 0x27fb47b9 185 1.1 mrg }; 186 1.1 mrg 187 1.1 mrg /* FPTR and RPTR are two pointers into the state info, a front and a rear 188 1.1 mrg pointer. These two pointers are always rand_sep places aparts, as they 189 1.1 mrg cycle through the state information. (Yes, this does mean we could get 190 1.1 mrg away with just one pointer, but the code for random is more efficient 191 1.1 mrg this way). The pointers are left positioned as they would be from the call: 192 1.1 mrg initstate(1, randtbl, 128); 193 1.1 mrg (The position of the rear pointer, rptr, is really 0 (as explained above 194 1.1 mrg in the initialization of randtbl) because the state table pointer is set 195 1.1 mrg to point to randtbl[1] (as explained below).) */ 196 1.1 mrg 197 1.1 mrg static long int *fptr = &randtbl[SEP_3 + 1]; 198 1.1 mrg static long int *rptr = &randtbl[1]; 199 1.1 mrg 200 1.1 mrg 201 1.1 mrg 202 1.1 mrg /* The following things are the pointer to the state information table, 203 1.1 mrg the type of the current generator, the degree of the current polynomial 204 1.1 mrg being used, and the separation between the two pointers. 205 1.1 mrg Note that for efficiency of random, we remember the first location of 206 1.1 mrg the state information, not the zeroeth. Hence it is valid to access 207 1.1 mrg state[-1], which is used to store the type of the R.N.G. 208 1.1 mrg Also, we remember the last location, since this is more efficient than 209 1.1 mrg indexing every time to find the address of the last element to see if 210 1.1 mrg the front and rear pointers have wrapped. */ 211 1.1 mrg 212 1.1 mrg static long int *state = &randtbl[1]; 213 1.1 mrg 214 1.1 mrg static int rand_type = TYPE_3; 215 1.1 mrg static int rand_deg = DEG_3; 216 1.1 mrg static int rand_sep = SEP_3; 217 1.1 mrg 218 1.1 mrg static long int *end_ptr = &randtbl[sizeof(randtbl) / sizeof(randtbl[0])]; 219 1.1 mrg 220 1.1 mrg /* Initialize the random number generator based on the given seed. If the 222 1.1 mrg type is the trivial no-state-information type, just remember the seed. 223 1.1 mrg Otherwise, initializes state[] based on the given "seed" via a linear 224 1.1 mrg congruential generator. Then, the pointers are set to known locations 225 1.1 mrg that are exactly rand_sep places apart. Lastly, it cycles the state 226 1.1 mrg information a given number of times to get rid of any initial dependencies 227 1.1 mrg introduced by the L.C.R.N.G. Note that the initialization of randtbl[] 228 1.1 mrg for default usage relies on values produced by this routine. */ 229 1.1 mrg void 230 1.1 mrg srandom (unsigned int x) 231 1.1 mrg { 232 1.1 mrg state[0] = x; 233 1.1 mrg if (rand_type != TYPE_0) 234 1.1 mrg { 235 1.1 mrg register long int i; 236 1.1 mrg for (i = 1; i < rand_deg; ++i) 237 1.1 mrg state[i] = (1103515145 * state[i - 1]) + 12345; 238 1.1 mrg fptr = &state[rand_sep]; 239 1.1 mrg rptr = &state[0]; 240 1.1 mrg for (i = 0; i < 10 * rand_deg; ++i) 241 1.1 mrg random(); 242 1.1 mrg } 243 1.1 mrg } 244 1.1 mrg 245 1.1 mrg /* Initialize the state information in the given array of N bytes for 247 1.1 mrg future random number generation. Based on the number of bytes we 248 1.1 mrg are given, and the break values for the different R.N.G.'s, we choose 249 1.1 mrg the best (largest) one we can and set things up for it. srandom is 250 1.1 mrg then called to initialize the state information. Note that on return 251 1.1 mrg from srandom, we set state[-1] to be the type multiplexed with the current 252 1.1 mrg value of the rear pointer; this is so successive calls to initstate won't 253 1.1 mrg lose this information and will be able to restart with setstate. 254 1.1 mrg Note: The first thing we do is save the current state, if any, just like 255 1.1.1.3 mrg setstate so that it doesn't matter when initstate is called. 256 1.1.1.3 mrg Returns a pointer to the old state. */ 257 1.1 mrg void * 258 1.1.1.3 mrg initstate (unsigned int seed, void *arg_state, unsigned long n) 259 1.1 mrg { 260 1.1 mrg void *ostate = (void *) &state[-1]; 261 1.1 mrg 262 1.1 mrg if (rand_type == TYPE_0) 263 1.1 mrg state[-1] = rand_type; 264 1.1 mrg else 265 1.1 mrg state[-1] = (MAX_TYPES * (rptr - state)) + rand_type; 266 1.1 mrg if (n < BREAK_1) 267 1.1 mrg { 268 1.1 mrg if (n < BREAK_0) 269 1.1 mrg { 270 1.1 mrg errno = EINVAL; 271 1.1 mrg return NULL; 272 1.1 mrg } 273 1.1 mrg rand_type = TYPE_0; 274 1.1 mrg rand_deg = DEG_0; 275 1.1 mrg rand_sep = SEP_0; 276 1.1 mrg } 277 1.1 mrg else if (n < BREAK_2) 278 1.1 mrg { 279 1.1 mrg rand_type = TYPE_1; 280 1.1 mrg rand_deg = DEG_1; 281 1.1 mrg rand_sep = SEP_1; 282 1.1 mrg } 283 1.1 mrg else if (n < BREAK_3) 284 1.1 mrg { 285 1.1 mrg rand_type = TYPE_2; 286 1.1 mrg rand_deg = DEG_2; 287 1.1 mrg rand_sep = SEP_2; 288 1.1 mrg } 289 1.1 mrg else if (n < BREAK_4) 290 1.1 mrg { 291 1.1 mrg rand_type = TYPE_3; 292 1.1 mrg rand_deg = DEG_3; 293 1.1 mrg rand_sep = SEP_3; 294 1.1 mrg } 295 1.1 mrg else 296 1.1 mrg { 297 1.1 mrg rand_type = TYPE_4; 298 1.1 mrg rand_deg = DEG_4; 299 1.1 mrg rand_sep = SEP_4; 300 1.1 mrg } 301 1.1 mrg 302 1.1 mrg state = &((long int *) arg_state)[1]; /* First location. */ 303 1.1 mrg /* Must set END_PTR before srandom. */ 304 1.1 mrg end_ptr = &state[rand_deg]; 305 1.1 mrg srandom(seed); 306 1.1 mrg if (rand_type == TYPE_0) 307 1.1 mrg state[-1] = rand_type; 308 1.1 mrg else 309 1.1 mrg state[-1] = (MAX_TYPES * (rptr - state)) + rand_type; 310 1.1 mrg 311 1.1 mrg return ostate; 312 1.1 mrg } 313 1.1 mrg 314 1.1 mrg /* Restore the state from the given state array. 316 1.1 mrg Note: It is important that we also remember the locations of the pointers 317 1.1 mrg in the current state information, and restore the locations of the pointers 318 1.1 mrg from the old state information. This is done by multiplexing the pointer 319 1.1 mrg location into the zeroeth word of the state information. Note that due 320 1.1 mrg to the order in which things are done, it is OK to call setstate with the 321 1.1.1.3 mrg same state as the current state 322 1.1.1.3 mrg Returns a pointer to the old state information. */ 323 1.1 mrg 324 1.1 mrg void * 325 1.1 mrg setstate (void *arg_state) 326 1.1 mrg { 327 1.1.1.3 mrg register long int *new_state = (long int *) arg_state; 328 1.1 mrg register int type = new_state[0] % MAX_TYPES; 329 1.1 mrg register int rear = new_state[0] / MAX_TYPES; 330 1.1 mrg void *ostate = (void *) &state[-1]; 331 1.1 mrg 332 1.1 mrg if (rand_type == TYPE_0) 333 1.1 mrg state[-1] = rand_type; 334 1.1 mrg else 335 1.1 mrg state[-1] = (MAX_TYPES * (rptr - state)) + rand_type; 336 1.1 mrg 337 1.1 mrg switch (type) 338 1.1 mrg { 339 1.1 mrg case TYPE_0: 340 1.1 mrg case TYPE_1: 341 1.1 mrg case TYPE_2: 342 1.1 mrg case TYPE_3: 343 1.1 mrg case TYPE_4: 344 1.1 mrg rand_type = type; 345 1.1 mrg rand_deg = degrees[type]; 346 1.1 mrg rand_sep = seps[type]; 347 1.1 mrg break; 348 1.1 mrg default: 349 1.1 mrg /* State info munged. */ 350 1.1 mrg errno = EINVAL; 351 1.1 mrg return NULL; 352 1.1 mrg } 353 1.1 mrg 354 1.1 mrg state = &new_state[1]; 355 1.1 mrg if (rand_type != TYPE_0) 356 1.1 mrg { 357 1.1 mrg rptr = &state[rear]; 358 1.1 mrg fptr = &state[(rear + rand_sep) % rand_deg]; 359 1.1 mrg } 360 1.1 mrg /* Set end_ptr too. */ 361 1.1 mrg end_ptr = &state[rand_deg]; 362 1.1 mrg 363 1.1 mrg return ostate; 364 1.1 mrg } 365 1.1 mrg 366 1.1 mrg /* If we are using the trivial TYPE_0 R.N.G., just do the old linear 368 1.1 mrg congruential bit. Otherwise, we do our fancy trinomial stuff, which is the 369 1.1 mrg same in all ther other cases due to all the global variables that have been 370 1.1 mrg set up. The basic operation is to add the number at the rear pointer into 371 1.1 mrg the one at the front pointer. Then both pointers are advanced to the next 372 1.1 mrg location cyclically in the table. The value returned is the sum generated, 373 1.1 mrg reduced to 31 bits by throwing away the "least random" low bit. 374 1.1 mrg Note: The code takes advantage of the fact that both the front and 375 1.1 mrg rear pointers can't wrap on the same call by not testing the rear 376 1.1 mrg pointer if the front one has wrapped. Returns a 31-bit random number. */ 377 1.1 mrg 378 1.1 mrg long int 379 1.1 mrg random (void) 380 1.1 mrg { 381 1.1 mrg if (rand_type == TYPE_0) 382 1.1 mrg { 383 1.1 mrg state[0] = ((state[0] * 1103515245) + 12345) & LONG_MAX; 384 1.1 mrg return state[0]; 385 1.1 mrg } 386 1.1 mrg else 387 1.1 mrg { 388 1.1 mrg long int i; 389 1.1 mrg *fptr += *rptr; 390 1.1 mrg /* Chucking least random bit. */ 391 1.1 mrg i = (*fptr >> 1) & LONG_MAX; 392 1.1 mrg ++fptr; 393 1.1 mrg if (fptr >= end_ptr) 394 1.1 mrg { 395 1.1 mrg fptr = state; 396 1.1 mrg ++rptr; 397 1.1 mrg } 398 1.1 mrg else 399 1.1 mrg { 400 1.1 mrg ++rptr; 401 1.1 mrg if (rptr >= end_ptr) 402 1.1 mrg rptr = state; 403 } 404 return i; 405 } 406 } 407