121354Sdist /* 221354Sdist * Copyright (c) 1983 Regents of the University of California. 334955Sbostic * All rights reserved. 434955Sbostic * 542625Sbostic * %sccs.include.redist.c% 621354Sdist */ 721354Sdist 826578Sdonn #if defined(LIBC_SCCS) && !defined(lint) 9*46573Sdonn static char sccsid[] = "@(#)random.c 5.8 (Berkeley) 02/23/91"; 1034955Sbostic #endif /* LIBC_SCCS and not lint */ 119316Smckusick 1234955Sbostic #include <stdio.h> 13*46573Sdonn #include <stdlib.h> 149316Smckusick 159316Smckusick /* 169316Smckusick * random.c: 179316Smckusick * An improved random number generation package. In addition to the standard 189316Smckusick * rand()/srand() like interface, this package also has a special state info 199316Smckusick * interface. The initstate() routine is called with a seed, an array of 209316Smckusick * bytes, and a count of how many bytes are being passed in; this array is then 219316Smckusick * initialized to contain information for random number generation with that 229316Smckusick * much state information. Good sizes for the amount of state information are 239316Smckusick * 32, 64, 128, and 256 bytes. The state can be switched by calling the 249316Smckusick * setstate() routine with the same array as was initiallized with initstate(). 259316Smckusick * By default, the package runs with 128 bytes of state information and 269316Smckusick * generates far better random numbers than a linear congruential generator. 279316Smckusick * If the amount of state information is less than 32 bytes, a simple linear 289316Smckusick * congruential R.N.G. is used. 299316Smckusick * Internally, the state information is treated as an array of longs; the 309316Smckusick * zeroeth element of the array is the type of R.N.G. being used (small 319316Smckusick * integer); the remainder of the array is the state information for the 329316Smckusick * R.N.G. Thus, 32 bytes of state information will give 7 longs worth of 339316Smckusick * state information, which will allow a degree seven polynomial. (Note: the 349316Smckusick * zeroeth word of state information also has some other information stored 359316Smckusick * in it -- see setstate() for details). 369316Smckusick * The random number generation technique is a linear feedback shift register 379316Smckusick * approach, employing trinomials (since there are fewer terms to sum up that 389316Smckusick * way). In this approach, the least significant bit of all the numbers in 399316Smckusick * the state table will act as a linear feedback shift register, and will have 409316Smckusick * period 2^deg - 1 (where deg is the degree of the polynomial being used, 419316Smckusick * assuming that the polynomial is irreducible and primitive). The higher 429316Smckusick * order bits will have longer periods, since their values are also influenced 439316Smckusick * by pseudo-random carries out of the lower bits. The total period of the 449316Smckusick * generator is approximately deg*(2**deg - 1); thus doubling the amount of 459316Smckusick * state information has a vast influence on the period of the generator. 469316Smckusick * Note: the deg*(2**deg - 1) is an approximation only good for large deg, 479316Smckusick * when the period of the shift register is the dominant factor. With deg 489316Smckusick * equal to seven, the period is actually much longer than the 7*(2**7 - 1) 499316Smckusick * predicted by this formula. 509316Smckusick */ 519316Smckusick 529316Smckusick 539316Smckusick 549316Smckusick /* 559316Smckusick * For each of the currently supported random number generators, we have a 569316Smckusick * break value on the amount of state information (you need at least this 579316Smckusick * many bytes of state info to support this random number generator), a degree 589316Smckusick * for the polynomial (actually a trinomial) that the R.N.G. is based on, and 599316Smckusick * the separation between the two lower order coefficients of the trinomial. 609316Smckusick */ 619316Smckusick 629316Smckusick #define TYPE_0 0 /* linear congruential */ 639316Smckusick #define BREAK_0 8 649316Smckusick #define DEG_0 0 659316Smckusick #define SEP_0 0 669316Smckusick 679316Smckusick #define TYPE_1 1 /* x**7 + x**3 + 1 */ 689316Smckusick #define BREAK_1 32 699316Smckusick #define DEG_1 7 709316Smckusick #define SEP_1 3 719316Smckusick 729316Smckusick #define TYPE_2 2 /* x**15 + x + 1 */ 739316Smckusick #define BREAK_2 64 749316Smckusick #define DEG_2 15 759316Smckusick #define SEP_2 1 769316Smckusick 779316Smckusick #define TYPE_3 3 /* x**31 + x**3 + 1 */ 789316Smckusick #define BREAK_3 128 799316Smckusick #define DEG_3 31 809316Smckusick #define SEP_3 3 819316Smckusick 829316Smckusick #define TYPE_4 4 /* x**63 + x + 1 */ 839316Smckusick #define BREAK_4 256 849316Smckusick #define DEG_4 63 859316Smckusick #define SEP_4 1 869316Smckusick 879316Smckusick 889316Smckusick /* 899316Smckusick * Array versions of the above information to make code run faster -- relies 909316Smckusick * on fact that TYPE_i == i. 919316Smckusick */ 929316Smckusick 939316Smckusick #define MAX_TYPES 5 /* max number of types above */ 949316Smckusick 959316Smckusick static int degrees[ MAX_TYPES ] = { DEG_0, DEG_1, DEG_2, 969316Smckusick DEG_3, DEG_4 }; 979316Smckusick 989316Smckusick static int seps[ MAX_TYPES ] = { SEP_0, SEP_1, SEP_2, 999316Smckusick SEP_3, SEP_4 }; 1009316Smckusick 1019316Smckusick 1029316Smckusick 1039316Smckusick /* 1049316Smckusick * Initially, everything is set up as if from : 1059316Smckusick * initstate( 1, &randtbl, 128 ); 1069316Smckusick * Note that this initialization takes advantage of the fact that srandom() 1079316Smckusick * advances the front and rear pointers 10*rand_deg times, and hence the 1089316Smckusick * rear pointer which starts at 0 will also end up at zero; thus the zeroeth 1099316Smckusick * element of the state information, which contains info about the current 1109316Smckusick * position of the rear pointer is just 1119316Smckusick * MAX_TYPES*(rptr - state) + TYPE_3 == TYPE_3. 1129316Smckusick */ 1139316Smckusick 1149316Smckusick static long randtbl[ DEG_3 + 1 ] = { TYPE_3, 1159316Smckusick 0x9a319039, 0x32d9c024, 0x9b663182, 0x5da1f342, 1169316Smckusick 0xde3b81e0, 0xdf0a6fb5, 0xf103bc02, 0x48f340fb, 1179316Smckusick 0x7449e56b, 0xbeb1dbb0, 0xab5c5918, 0x946554fd, 1189316Smckusick 0x8c2e680f, 0xeb3d799f, 0xb11ee0b7, 0x2d436b86, 1199316Smckusick 0xda672e2a, 0x1588ca88, 0xe369735d, 0x904f35f7, 1209316Smckusick 0xd7158fd6, 0x6fa6f051, 0x616e6b96, 0xac94efdc, 1219316Smckusick 0x36413f93, 0xc622c298, 0xf5a42ab8, 0x8a88d77b, 1229316Smckusick 0xf5ad9d0e, 0x8999220b, 0x27fb47b9 }; 1239316Smckusick 1249316Smckusick /* 1259316Smckusick * fptr and rptr are two pointers into the state info, a front and a rear 1269316Smckusick * pointer. These two pointers are always rand_sep places aparts, as they cycle 1279316Smckusick * cyclically through the state information. (Yes, this does mean we could get 1289316Smckusick * away with just one pointer, but the code for random() is more efficient this 1299316Smckusick * way). The pointers are left positioned as they would be from the call 1309316Smckusick * initstate( 1, randtbl, 128 ) 1319316Smckusick * (The position of the rear pointer, rptr, is really 0 (as explained above 1329316Smckusick * in the initialization of randtbl) because the state table pointer is set 1339316Smckusick * to point to randtbl[1] (as explained below). 1349316Smckusick */ 1359316Smckusick 1369316Smckusick static long *fptr = &randtbl[ SEP_3 + 1 ]; 1379316Smckusick static long *rptr = &randtbl[ 1 ]; 1389316Smckusick 1399316Smckusick 1409316Smckusick 1419316Smckusick /* 1429316Smckusick * The following things are the pointer to the state information table, 1439316Smckusick * the type of the current generator, the degree of the current polynomial 1449316Smckusick * being used, and the separation between the two pointers. 1459316Smckusick * Note that for efficiency of random(), we remember the first location of 1469316Smckusick * the state information, not the zeroeth. Hence it is valid to access 1479316Smckusick * state[-1], which is used to store the type of the R.N.G. 1489316Smckusick * Also, we remember the last location, since this is more efficient than 1499316Smckusick * indexing every time to find the address of the last element to see if 1509316Smckusick * the front and rear pointers have wrapped. 1519316Smckusick */ 1529316Smckusick 15316396Searl static long *state = &randtbl[ 1 ]; 1549316Smckusick 1559316Smckusick static int rand_type = TYPE_3; 1569316Smckusick static int rand_deg = DEG_3; 1579316Smckusick static int rand_sep = SEP_3; 1589316Smckusick 1599316Smckusick static long *end_ptr = &randtbl[ DEG_3 + 1 ]; 1609316Smckusick 1619316Smckusick 1629316Smckusick 1639316Smckusick /* 1649316Smckusick * srandom: 1659316Smckusick * Initialize the random number generator based on the given seed. If the 1669316Smckusick * type is the trivial no-state-information type, just remember the seed. 1679316Smckusick * Otherwise, initializes state[] based on the given "seed" via a linear 1689316Smckusick * congruential generator. Then, the pointers are set to known locations 1699316Smckusick * that are exactly rand_sep places apart. Lastly, it cycles the state 1709316Smckusick * information a given number of times to get rid of any initial dependencies 1719316Smckusick * introduced by the L.C.R.N.G. 1729316Smckusick * Note that the initialization of randtbl[] for default usage relies on 1739316Smckusick * values produced by this routine. 1749316Smckusick */ 1759316Smckusick 176*46573Sdonn void 1779316Smckusick srandom( x ) 1789316Smckusick 1799316Smckusick unsigned x; 1809316Smckusick { 1819316Smckusick register int i, j; 1829316Smckusick 1839316Smckusick if( rand_type == TYPE_0 ) { 1849316Smckusick state[ 0 ] = x; 1859316Smckusick } 1869316Smckusick else { 1879316Smckusick j = 1; 1889316Smckusick state[ 0 ] = x; 1899316Smckusick for( i = 1; i < rand_deg; i++ ) { 1909316Smckusick state[i] = 1103515245*state[i - 1] + 12345; 1919316Smckusick } 1929316Smckusick fptr = &state[ rand_sep ]; 1939316Smckusick rptr = &state[ 0 ]; 194*46573Sdonn for( i = 0; i < 10*rand_deg; i++ ) 195*46573Sdonn (void) random(); 1969316Smckusick } 1979316Smckusick } 1989316Smckusick 1999316Smckusick 2009316Smckusick 2019316Smckusick /* 2029316Smckusick * initstate: 2039316Smckusick * Initialize the state information in the given array of n bytes for 2049316Smckusick * future random number generation. Based on the number of bytes we 2059316Smckusick * are given, and the break values for the different R.N.G.'s, we choose 2069316Smckusick * the best (largest) one we can and set things up for it. srandom() is 2079316Smckusick * then called to initialize the state information. 2089316Smckusick * Note that on return from srandom(), we set state[-1] to be the type 2099316Smckusick * multiplexed with the current value of the rear pointer; this is so 2109316Smckusick * successive calls to initstate() won't lose this information and will 2119316Smckusick * be able to restart with setstate(). 2129316Smckusick * Note: the first thing we do is save the current state, if any, just like 2139316Smckusick * setstate() so that it doesn't matter when initstate is called. 2149316Smckusick * Returns a pointer to the old state. 2159316Smckusick */ 2169316Smckusick 2179316Smckusick char * 2189316Smckusick initstate( seed, arg_state, n ) 2199316Smckusick 2209316Smckusick unsigned seed; /* seed for R. N. G. */ 2219316Smckusick char *arg_state; /* pointer to state array */ 2229316Smckusick int n; /* # bytes of state info */ 2239316Smckusick { 2249316Smckusick register char *ostate = (char *)( &state[ -1 ] ); 2259316Smckusick 2269316Smckusick if( rand_type == TYPE_0 ) state[ -1 ] = rand_type; 2279316Smckusick else state[ -1 ] = MAX_TYPES*(rptr - state) + rand_type; 2289316Smckusick if( n < BREAK_1 ) { 2299316Smckusick if( n < BREAK_0 ) { 23039837Smckusick fprintf( stderr, "initstate: not enough state (%d bytes); ignored.\n", n ); 23139837Smckusick return 0; 2329316Smckusick } 2339316Smckusick rand_type = TYPE_0; 2349316Smckusick rand_deg = DEG_0; 2359316Smckusick rand_sep = SEP_0; 2369316Smckusick } 2379316Smckusick else { 2389316Smckusick if( n < BREAK_2 ) { 2399316Smckusick rand_type = TYPE_1; 2409316Smckusick rand_deg = DEG_1; 2419316Smckusick rand_sep = SEP_1; 2429316Smckusick } 2439316Smckusick else { 2449316Smckusick if( n < BREAK_3 ) { 2459316Smckusick rand_type = TYPE_2; 2469316Smckusick rand_deg = DEG_2; 2479316Smckusick rand_sep = SEP_2; 2489316Smckusick } 2499316Smckusick else { 2509316Smckusick if( n < BREAK_4 ) { 2519316Smckusick rand_type = TYPE_3; 2529316Smckusick rand_deg = DEG_3; 2539316Smckusick rand_sep = SEP_3; 2549316Smckusick } 2559316Smckusick else { 2569316Smckusick rand_type = TYPE_4; 2579316Smckusick rand_deg = DEG_4; 2589316Smckusick rand_sep = SEP_4; 2599316Smckusick } 2609316Smckusick } 2619316Smckusick } 2629316Smckusick } 2639316Smckusick state = &( ( (long *)arg_state )[1] ); /* first location */ 2649316Smckusick end_ptr = &state[ rand_deg ]; /* must set end_ptr before srandom */ 2659316Smckusick srandom( seed ); 2669316Smckusick if( rand_type == TYPE_0 ) state[ -1 ] = rand_type; 2679316Smckusick else state[ -1 ] = MAX_TYPES*(rptr - state) + rand_type; 2689316Smckusick return( ostate ); 2699316Smckusick } 2709316Smckusick 2719316Smckusick 2729316Smckusick 2739316Smckusick /* 2749316Smckusick * setstate: 2759316Smckusick * Restore the state from the given state array. 2769316Smckusick * Note: it is important that we also remember the locations of the pointers 2779316Smckusick * in the current state information, and restore the locations of the pointers 2789316Smckusick * from the old state information. This is done by multiplexing the pointer 2799316Smckusick * location into the zeroeth word of the state information. 2809316Smckusick * Note that due to the order in which things are done, it is OK to call 2819316Smckusick * setstate() with the same state as the current state. 2829316Smckusick * Returns a pointer to the old state information. 2839316Smckusick */ 2849316Smckusick 2859316Smckusick char * 2869316Smckusick setstate( arg_state ) 2879316Smckusick 2889316Smckusick char *arg_state; 2899316Smckusick { 2909316Smckusick register long *new_state = (long *)arg_state; 2919316Smckusick register int type = new_state[0]%MAX_TYPES; 2929316Smckusick register int rear = new_state[0]/MAX_TYPES; 2939316Smckusick char *ostate = (char *)( &state[ -1 ] ); 2949316Smckusick 2959316Smckusick if( rand_type == TYPE_0 ) state[ -1 ] = rand_type; 2969316Smckusick else state[ -1 ] = MAX_TYPES*(rptr - state) + rand_type; 2979316Smckusick switch( type ) { 2989316Smckusick case TYPE_0: 2999316Smckusick case TYPE_1: 3009316Smckusick case TYPE_2: 3019316Smckusick case TYPE_3: 3029316Smckusick case TYPE_4: 3039316Smckusick rand_type = type; 3049316Smckusick rand_deg = degrees[ type ]; 3059316Smckusick rand_sep = seps[ type ]; 3069316Smckusick break; 3079316Smckusick 3089316Smckusick default: 3099316Smckusick fprintf( stderr, "setstate: state info has been munged; not changed.\n" ); 3109316Smckusick } 3119316Smckusick state = &new_state[ 1 ]; 3129316Smckusick if( rand_type != TYPE_0 ) { 3139316Smckusick rptr = &state[ rear ]; 3149316Smckusick fptr = &state[ (rear + rand_sep)%rand_deg ]; 3159316Smckusick } 3169316Smckusick end_ptr = &state[ rand_deg ]; /* set end_ptr too */ 3179316Smckusick return( ostate ); 3189316Smckusick } 3199316Smckusick 3209316Smckusick 3219316Smckusick 3229316Smckusick /* 3239316Smckusick * random: 3249316Smckusick * If we are using the trivial TYPE_0 R.N.G., just do the old linear 3259316Smckusick * congruential bit. Otherwise, we do our fancy trinomial stuff, which is the 3269316Smckusick * same in all ther other cases due to all the global variables that have been 3279316Smckusick * set up. The basic operation is to add the number at the rear pointer into 3289316Smckusick * the one at the front pointer. Then both pointers are advanced to the next 3299316Smckusick * location cyclically in the table. The value returned is the sum generated, 3309316Smckusick * reduced to 31 bits by throwing away the "least random" low bit. 3319316Smckusick * Note: the code takes advantage of the fact that both the front and 3329316Smckusick * rear pointers can't wrap on the same call by not testing the rear 3339316Smckusick * pointer if the front one has wrapped. 3349316Smckusick * Returns a 31-bit random number. 3359316Smckusick */ 3369316Smckusick 3379316Smckusick long 3389316Smckusick random() 3399316Smckusick { 3409316Smckusick long i; 3419316Smckusick 3429316Smckusick if( rand_type == TYPE_0 ) { 3439316Smckusick i = state[0] = ( state[0]*1103515245 + 12345 )&0x7fffffff; 3449316Smckusick } 3459316Smckusick else { 3469316Smckusick *fptr += *rptr; 3479316Smckusick i = (*fptr >> 1)&0x7fffffff; /* chucking least random bit */ 3489316Smckusick if( ++fptr >= end_ptr ) { 3499316Smckusick fptr = state; 3509316Smckusick ++rptr; 3519316Smckusick } 3529316Smckusick else { 3539316Smckusick if( ++rptr >= end_ptr ) rptr = state; 3549316Smckusick } 3559316Smckusick } 3569316Smckusick return( i ); 3579316Smckusick } 358