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