121354Sdist /* 2*61180Sbostic * Copyright (c) 1983, 1993 3*61180Sbostic * The Regents of the University of California. All rights reserved. 434955Sbostic * 542625Sbostic * %sccs.include.redist.c% 621354Sdist */ 721354Sdist 826578Sdonn #if defined(LIBC_SCCS) && !defined(lint) 9*61180Sbostic static char sccsid[] = "@(#)random.c 8.1 (Berkeley) 06/04/93"; 1034955Sbostic #endif /* LIBC_SCCS and not lint */ 119316Smckusick 1234955Sbostic #include <stdio.h> 1346573Sdonn #include <stdlib.h> 149316Smckusick 159316Smckusick /* 169316Smckusick * random.c: 1746575Sbostic * 189316Smckusick * An improved random number generation package. In addition to the standard 199316Smckusick * rand()/srand() like interface, this package also has a special state info 209316Smckusick * interface. The initstate() routine is called with a seed, an array of 2146575Sbostic * bytes, and a count of how many bytes are being passed in; this array is 2246575Sbostic * then initialized to contain information for random number generation with 2346575Sbostic * that much state information. Good sizes for the amount of state 2446575Sbostic * information are 32, 64, 128, and 256 bytes. The state can be switched by 2546575Sbostic * calling the setstate() routine with the same array as was initiallized 2646575Sbostic * with initstate(). By default, the package runs with 128 bytes of state 2746575Sbostic * information and generates far better random numbers than a linear 2846575Sbostic * congruential generator. If the amount of state information is less than 2946575Sbostic * 32 bytes, a simple linear congruential R.N.G. is used. 3046575Sbostic * 319316Smckusick * Internally, the state information is treated as an array of longs; the 329316Smckusick * zeroeth element of the array is the type of R.N.G. being used (small 339316Smckusick * integer); the remainder of the array is the state information for the 349316Smckusick * R.N.G. Thus, 32 bytes of state information will give 7 longs worth of 3546575Sbostic * state information, which will allow a degree seven polynomial. (Note: 3646575Sbostic * the zeroeth word of state information also has some other information 3746575Sbostic * stored in it -- see setstate() for details). 3846575Sbostic * 399316Smckusick * The random number generation technique is a linear feedback shift register 409316Smckusick * approach, employing trinomials (since there are fewer terms to sum up that 419316Smckusick * way). In this approach, the least significant bit of all the numbers in 4246575Sbostic * the state table will act as a linear feedback shift register, and will 4346575Sbostic * have period 2^deg - 1 (where deg is the degree of the polynomial being 4446575Sbostic * used, assuming that the polynomial is irreducible and primitive). The 4546575Sbostic * higher order bits will have longer periods, since their values are also 4646575Sbostic * influenced by pseudo-random carries out of the lower bits. The total 4746575Sbostic * period of the generator is approximately deg*(2**deg - 1); thus doubling 4846575Sbostic * the amount of state information has a vast influence on the period of the 4946575Sbostic * generator. Note: the deg*(2**deg - 1) is an approximation only good for 5046575Sbostic * large deg, when the period of the shift register is the dominant factor. 5146575Sbostic * With deg equal to seven, the period is actually much longer than the 5246575Sbostic * 7*(2**7 - 1) predicted by this formula. 539316Smckusick */ 549316Smckusick 559316Smckusick /* 569316Smckusick * For each of the currently supported random number generators, we have a 579316Smckusick * break value on the amount of state information (you need at least this 589316Smckusick * many bytes of state info to support this random number generator), a degree 599316Smckusick * for the polynomial (actually a trinomial) that the R.N.G. is based on, and 609316Smckusick * the separation between the two lower order coefficients of the trinomial. 619316Smckusick */ 6246575Sbostic #define TYPE_0 0 /* linear congruential */ 6346575Sbostic #define BREAK_0 8 6446575Sbostic #define DEG_0 0 6546575Sbostic #define SEP_0 0 669316Smckusick 6746575Sbostic #define TYPE_1 1 /* x**7 + x**3 + 1 */ 6846575Sbostic #define BREAK_1 32 6946575Sbostic #define DEG_1 7 7046575Sbostic #define SEP_1 3 719316Smckusick 7246575Sbostic #define TYPE_2 2 /* x**15 + x + 1 */ 7346575Sbostic #define BREAK_2 64 7446575Sbostic #define DEG_2 15 7546575Sbostic #define SEP_2 1 769316Smckusick 7746575Sbostic #define TYPE_3 3 /* x**31 + x**3 + 1 */ 7846575Sbostic #define BREAK_3 128 7946575Sbostic #define DEG_3 31 8046575Sbostic #define SEP_3 3 819316Smckusick 8246575Sbostic #define TYPE_4 4 /* x**63 + x + 1 */ 8346575Sbostic #define BREAK_4 256 8446575Sbostic #define DEG_4 63 8546575Sbostic #define SEP_4 1 869316Smckusick 879316Smckusick /* 8846575Sbostic * Array versions of the above information to make code run faster -- 8946575Sbostic * relies on fact that TYPE_i == i. 909316Smckusick */ 9146575Sbostic #define MAX_TYPES 5 /* max number of types above */ 929316Smckusick 9346575Sbostic static int degrees[MAX_TYPES] = { DEG_0, DEG_1, DEG_2, DEG_3, DEG_4 }; 9446575Sbostic static int seps [MAX_TYPES] = { SEP_0, SEP_1, SEP_2, SEP_3, SEP_4 }; 959316Smckusick 969316Smckusick /* 9746575Sbostic * Initially, everything is set up as if from: 9846575Sbostic * 9946575Sbostic * initstate(1, &randtbl, 128); 10046575Sbostic * 1019316Smckusick * Note that this initialization takes advantage of the fact that srandom() 1029316Smckusick * advances the front and rear pointers 10*rand_deg times, and hence the 1039316Smckusick * rear pointer which starts at 0 will also end up at zero; thus the zeroeth 1049316Smckusick * element of the state information, which contains info about the current 1059316Smckusick * position of the rear pointer is just 10646575Sbostic * 10746575Sbostic * MAX_TYPES * (rptr - state) + TYPE_3 == TYPE_3. 1089316Smckusick */ 1099316Smckusick 11046575Sbostic static long randtbl[DEG_3 + 1] = { 11146575Sbostic TYPE_3, 11246575Sbostic 0x9a319039, 0x32d9c024, 0x9b663182, 0x5da1f342, 0xde3b81e0, 0xdf0a6fb5, 11346575Sbostic 0xf103bc02, 0x48f340fb, 0x7449e56b, 0xbeb1dbb0, 0xab5c5918, 0x946554fd, 11446575Sbostic 0x8c2e680f, 0xeb3d799f, 0xb11ee0b7, 0x2d436b86, 0xda672e2a, 0x1588ca88, 11546575Sbostic 0xe369735d, 0x904f35f7, 0xd7158fd6, 0x6fa6f051, 0x616e6b96, 0xac94efdc, 11646575Sbostic 0x36413f93, 0xc622c298, 0xf5a42ab8, 0x8a88d77b, 0xf5ad9d0e, 0x8999220b, 11746575Sbostic 0x27fb47b9, 11846575Sbostic }; 1199316Smckusick 1209316Smckusick /* 1219316Smckusick * fptr and rptr are two pointers into the state info, a front and a rear 12246575Sbostic * pointer. These two pointers are always rand_sep places aparts, as they 12346575Sbostic * cycle cyclically through the state information. (Yes, this does mean we 12446575Sbostic * could get away with just one pointer, but the code for random() is more 12546575Sbostic * efficient this way). The pointers are left positioned as they would be 12646575Sbostic * from the call 12746575Sbostic * 12846575Sbostic * initstate(1, randtbl, 128); 12946575Sbostic * 1309316Smckusick * (The position of the rear pointer, rptr, is really 0 (as explained above 1319316Smckusick * in the initialization of randtbl) because the state table pointer is set 1329316Smckusick * to point to randtbl[1] (as explained below). 1339316Smckusick */ 13446575Sbostic static long *fptr = &randtbl[SEP_3 + 1]; 13546575Sbostic static long *rptr = &randtbl[1]; 1369316Smckusick 1379316Smckusick /* 13846575Sbostic * The following things are the pointer to the state information table, the 13946575Sbostic * type of the current generator, the degree of the current polynomial being 14046575Sbostic * used, and the separation between the two pointers. Note that for efficiency 14146575Sbostic * of random(), we remember the first location of the state information, not 14246575Sbostic * the zeroeth. Hence it is valid to access state[-1], which is used to 14346575Sbostic * store the type of the R.N.G. Also, we remember the last location, since 14446575Sbostic * this is more efficient than indexing every time to find the address of 14546575Sbostic * the last element to see if the front and rear pointers have wrapped. 1469316Smckusick */ 14746575Sbostic static long *state = &randtbl[1]; 14846575Sbostic static int rand_type = TYPE_3; 14946575Sbostic static int rand_deg = DEG_3; 15046575Sbostic static int rand_sep = SEP_3; 15146575Sbostic static long *end_ptr = &randtbl[DEG_3 + 1]; 1529316Smckusick 1539316Smckusick /* 1549316Smckusick * srandom: 15546575Sbostic * 1569316Smckusick * Initialize the random number generator based on the given seed. If the 1579316Smckusick * type is the trivial no-state-information type, just remember the seed. 1589316Smckusick * Otherwise, initializes state[] based on the given "seed" via a linear 1599316Smckusick * congruential generator. Then, the pointers are set to known locations 1609316Smckusick * that are exactly rand_sep places apart. Lastly, it cycles the state 1619316Smckusick * information a given number of times to get rid of any initial dependencies 16246575Sbostic * introduced by the L.C.R.N.G. Note that the initialization of randtbl[] 16346575Sbostic * for default usage relies on values produced by this routine. 1649316Smckusick */ 16546573Sdonn void 16646575Sbostic srandom(x) 16746575Sbostic u_int x; 1689316Smckusick { 16946575Sbostic register int i, j; 1709316Smckusick 17146575Sbostic if (rand_type == TYPE_0) 17246575Sbostic state[0] = x; 17346575Sbostic else { 17446575Sbostic j = 1; 17546575Sbostic state[0] = x; 17646575Sbostic for (i = 1; i < rand_deg; i++) 17746575Sbostic state[i] = 1103515245 * state[i - 1] + 12345; 17846575Sbostic fptr = &state[rand_sep]; 17946575Sbostic rptr = &state[0]; 18046575Sbostic for (i = 0; i < 10 * rand_deg; i++) 18146575Sbostic (void)random(); 1829316Smckusick } 1839316Smckusick } 1849316Smckusick 1859316Smckusick /* 1869316Smckusick * initstate: 18746575Sbostic * 18846575Sbostic * Initialize the state information in the given array of n bytes for future 18946575Sbostic * random number generation. Based on the number of bytes we are given, and 19046575Sbostic * the break values for the different R.N.G.'s, we choose the best (largest) 19146575Sbostic * one we can and set things up for it. srandom() is then called to 19246575Sbostic * initialize the state information. 19346575Sbostic * 1949316Smckusick * Note that on return from srandom(), we set state[-1] to be the type 1959316Smckusick * multiplexed with the current value of the rear pointer; this is so 19646575Sbostic * successive calls to initstate() won't lose this information and will be 19746575Sbostic * able to restart with setstate(). 19846575Sbostic * 1999316Smckusick * Note: the first thing we do is save the current state, if any, just like 2009316Smckusick * setstate() so that it doesn't matter when initstate is called. 20146575Sbostic * 2029316Smckusick * Returns a pointer to the old state. 2039316Smckusick */ 20446575Sbostic char * 20546575Sbostic initstate(seed, arg_state, n) 20646575Sbostic u_int seed; /* seed for R.N.G. */ 20746575Sbostic char *arg_state; /* pointer to state array */ 20846575Sbostic int n; /* # bytes of state info */ 2099316Smckusick { 21046575Sbostic register char *ostate = (char *)(&state[-1]); 2119316Smckusick 21246575Sbostic if (rand_type == TYPE_0) 21346575Sbostic state[-1] = rand_type; 21446575Sbostic else 21546575Sbostic state[-1] = MAX_TYPES * (rptr - state) + rand_type; 21646575Sbostic if (n < BREAK_0) { 21746575Sbostic (void)fprintf(stderr, 21846575Sbostic "random: not enough state (%d bytes); ignored.\n", n); 21946575Sbostic return(0); 2209316Smckusick } 22146575Sbostic if (n < BREAK_1) { 22246575Sbostic rand_type = TYPE_0; 22346575Sbostic rand_deg = DEG_0; 22446575Sbostic rand_sep = SEP_0; 22546575Sbostic } else if (n < BREAK_2) { 2269316Smckusick rand_type = TYPE_1; 2279316Smckusick rand_deg = DEG_1; 2289316Smckusick rand_sep = SEP_1; 22946575Sbostic } else if (n < BREAK_3) { 23046575Sbostic rand_type = TYPE_2; 23146575Sbostic rand_deg = DEG_2; 23246575Sbostic rand_sep = SEP_2; 23346575Sbostic } else if (n < BREAK_4) { 23446575Sbostic rand_type = TYPE_3; 23546575Sbostic rand_deg = DEG_3; 23646575Sbostic rand_sep = SEP_3; 23746575Sbostic } else { 23846575Sbostic rand_type = TYPE_4; 23946575Sbostic rand_deg = DEG_4; 24046575Sbostic rand_sep = SEP_4; 2419316Smckusick } 24246575Sbostic state = &(((long *)arg_state)[1]); /* first location */ 24346575Sbostic end_ptr = &state[rand_deg]; /* must set end_ptr before srandom */ 24446575Sbostic srandom(seed); 24546575Sbostic if (rand_type == TYPE_0) 24646575Sbostic state[-1] = rand_type; 24746575Sbostic else 24846575Sbostic state[-1] = MAX_TYPES*(rptr - state) + rand_type; 24946575Sbostic return(ostate); 2509316Smckusick } 2519316Smckusick 2529316Smckusick /* 2539316Smckusick * setstate: 25446575Sbostic * 2559316Smckusick * Restore the state from the given state array. 25646575Sbostic * 2579316Smckusick * Note: it is important that we also remember the locations of the pointers 2589316Smckusick * in the current state information, and restore the locations of the pointers 2599316Smckusick * from the old state information. This is done by multiplexing the pointer 2609316Smckusick * location into the zeroeth word of the state information. 26146575Sbostic * 2629316Smckusick * Note that due to the order in which things are done, it is OK to call 2639316Smckusick * setstate() with the same state as the current state. 26446575Sbostic * 2659316Smckusick * Returns a pointer to the old state information. 2669316Smckusick */ 26746575Sbostic char * 26846575Sbostic setstate(arg_state) 26946575Sbostic char *arg_state; 2709316Smckusick { 27146575Sbostic register long *new_state = (long *)arg_state; 27246575Sbostic register int type = new_state[0] % MAX_TYPES; 27346575Sbostic register int rear = new_state[0] / MAX_TYPES; 27446575Sbostic char *ostate = (char *)(&state[-1]); 2759316Smckusick 27646575Sbostic if (rand_type == TYPE_0) 27746575Sbostic state[-1] = rand_type; 27846575Sbostic else 27946575Sbostic state[-1] = MAX_TYPES * (rptr - state) + rand_type; 28046575Sbostic switch(type) { 28146575Sbostic case TYPE_0: 28246575Sbostic case TYPE_1: 28346575Sbostic case TYPE_2: 28446575Sbostic case TYPE_3: 28546575Sbostic case TYPE_4: 2869316Smckusick rand_type = type; 28746575Sbostic rand_deg = degrees[type]; 28846575Sbostic rand_sep = seps[type]; 2899316Smckusick break; 29046575Sbostic default: 29146575Sbostic (void)fprintf(stderr, 29246575Sbostic "random: state info corrupted; not changed.\n"); 2939316Smckusick } 29446575Sbostic state = &new_state[1]; 29546575Sbostic if (rand_type != TYPE_0) { 29646575Sbostic rptr = &state[rear]; 29746575Sbostic fptr = &state[(rear + rand_sep) % rand_deg]; 2989316Smckusick } 29946575Sbostic end_ptr = &state[rand_deg]; /* set end_ptr too */ 30046575Sbostic return(ostate); 3019316Smckusick } 3029316Smckusick 3039316Smckusick /* 3049316Smckusick * random: 30546575Sbostic * 3069316Smckusick * If we are using the trivial TYPE_0 R.N.G., just do the old linear 30746575Sbostic * congruential bit. Otherwise, we do our fancy trinomial stuff, which is 30846575Sbostic * the same in all the other cases due to all the global variables that have 30946575Sbostic * been set up. The basic operation is to add the number at the rear pointer 31046575Sbostic * into the one at the front pointer. Then both pointers are advanced to 31146575Sbostic * the next location cyclically in the table. The value returned is the sum 31246575Sbostic * generated, reduced to 31 bits by throwing away the "least random" low bit. 31346575Sbostic * 3149316Smckusick * Note: the code takes advantage of the fact that both the front and 3159316Smckusick * rear pointers can't wrap on the same call by not testing the rear 3169316Smckusick * pointer if the front one has wrapped. 31746575Sbostic * 3189316Smckusick * Returns a 31-bit random number. 3199316Smckusick */ 3209316Smckusick long 3219316Smckusick random() 3229316Smckusick { 32346575Sbostic long i; 32446575Sbostic 32546575Sbostic if (rand_type == TYPE_0) 32646575Sbostic i = state[0] = (state[0] * 1103515245 + 12345) & 0x7fffffff; 32746575Sbostic else { 32846575Sbostic *fptr += *rptr; 32946575Sbostic i = (*fptr >> 1) & 0x7fffffff; /* chucking least random bit */ 33046575Sbostic if (++fptr >= end_ptr) { 33146575Sbostic fptr = state; 33246575Sbostic ++rptr; 33346575Sbostic } else if (++rptr >= end_ptr) 33446575Sbostic rptr = state; 3359316Smckusick } 33646575Sbostic return(i); 3379316Smckusick } 338