1*9316Smckusick /* Copyright (c) 1982 Regents of the University of California */ 2*9316Smckusick 3*9316Smckusick static char sccsid[] = "@(#)random.c 4.1 (Berkeley) 82/11/22"; 4*9316Smckusick 5*9316Smckusick #include <stdio.h> 6*9316Smckusick 7*9316Smckusick /* 8*9316Smckusick * random.c: 9*9316Smckusick * An improved random number generation package. In addition to the standard 10*9316Smckusick * rand()/srand() like interface, this package also has a special state info 11*9316Smckusick * interface. The initstate() routine is called with a seed, an array of 12*9316Smckusick * bytes, and a count of how many bytes are being passed in; this array is then 13*9316Smckusick * initialized to contain information for random number generation with that 14*9316Smckusick * much state information. Good sizes for the amount of state information are 15*9316Smckusick * 32, 64, 128, and 256 bytes. The state can be switched by calling the 16*9316Smckusick * setstate() routine with the same array as was initiallized with initstate(). 17*9316Smckusick * By default, the package runs with 128 bytes of state information and 18*9316Smckusick * generates far better random numbers than a linear congruential generator. 19*9316Smckusick * If the amount of state information is less than 32 bytes, a simple linear 20*9316Smckusick * congruential R.N.G. is used. 21*9316Smckusick * Internally, the state information is treated as an array of longs; the 22*9316Smckusick * zeroeth element of the array is the type of R.N.G. being used (small 23*9316Smckusick * integer); the remainder of the array is the state information for the 24*9316Smckusick * R.N.G. Thus, 32 bytes of state information will give 7 longs worth of 25*9316Smckusick * state information, which will allow a degree seven polynomial. (Note: the 26*9316Smckusick * zeroeth word of state information also has some other information stored 27*9316Smckusick * in it -- see setstate() for details). 28*9316Smckusick * The random number generation technique is a linear feedback shift register 29*9316Smckusick * approach, employing trinomials (since there are fewer terms to sum up that 30*9316Smckusick * way). In this approach, the least significant bit of all the numbers in 31*9316Smckusick * the state table will act as a linear feedback shift register, and will have 32*9316Smckusick * period 2^deg - 1 (where deg is the degree of the polynomial being used, 33*9316Smckusick * assuming that the polynomial is irreducible and primitive). The higher 34*9316Smckusick * order bits will have longer periods, since their values are also influenced 35*9316Smckusick * by pseudo-random carries out of the lower bits. The total period of the 36*9316Smckusick * generator is approximately deg*(2**deg - 1); thus doubling the amount of 37*9316Smckusick * state information has a vast influence on the period of the generator. 38*9316Smckusick * Note: the deg*(2**deg - 1) is an approximation only good for large deg, 39*9316Smckusick * when the period of the shift register is the dominant factor. With deg 40*9316Smckusick * equal to seven, the period is actually much longer than the 7*(2**7 - 1) 41*9316Smckusick * predicted by this formula. 42*9316Smckusick */ 43*9316Smckusick 44*9316Smckusick 45*9316Smckusick 46*9316Smckusick /* 47*9316Smckusick * For each of the currently supported random number generators, we have a 48*9316Smckusick * break value on the amount of state information (you need at least this 49*9316Smckusick * many bytes of state info to support this random number generator), a degree 50*9316Smckusick * for the polynomial (actually a trinomial) that the R.N.G. is based on, and 51*9316Smckusick * the separation between the two lower order coefficients of the trinomial. 52*9316Smckusick */ 53*9316Smckusick 54*9316Smckusick #define TYPE_0 0 /* linear congruential */ 55*9316Smckusick #define BREAK_0 8 56*9316Smckusick #define DEG_0 0 57*9316Smckusick #define SEP_0 0 58*9316Smckusick 59*9316Smckusick #define TYPE_1 1 /* x**7 + x**3 + 1 */ 60*9316Smckusick #define BREAK_1 32 61*9316Smckusick #define DEG_1 7 62*9316Smckusick #define SEP_1 3 63*9316Smckusick 64*9316Smckusick #define TYPE_2 2 /* x**15 + x + 1 */ 65*9316Smckusick #define BREAK_2 64 66*9316Smckusick #define DEG_2 15 67*9316Smckusick #define SEP_2 1 68*9316Smckusick 69*9316Smckusick #define TYPE_3 3 /* x**31 + x**3 + 1 */ 70*9316Smckusick #define BREAK_3 128 71*9316Smckusick #define DEG_3 31 72*9316Smckusick #define SEP_3 3 73*9316Smckusick 74*9316Smckusick #define TYPE_4 4 /* x**63 + x + 1 */ 75*9316Smckusick #define BREAK_4 256 76*9316Smckusick #define DEG_4 63 77*9316Smckusick #define SEP_4 1 78*9316Smckusick 79*9316Smckusick 80*9316Smckusick /* 81*9316Smckusick * Array versions of the above information to make code run faster -- relies 82*9316Smckusick * on fact that TYPE_i == i. 83*9316Smckusick */ 84*9316Smckusick 85*9316Smckusick #define MAX_TYPES 5 /* max number of types above */ 86*9316Smckusick 87*9316Smckusick static int degrees[ MAX_TYPES ] = { DEG_0, DEG_1, DEG_2, 88*9316Smckusick DEG_3, DEG_4 }; 89*9316Smckusick 90*9316Smckusick static int seps[ MAX_TYPES ] = { SEP_0, SEP_1, SEP_2, 91*9316Smckusick SEP_3, SEP_4 }; 92*9316Smckusick 93*9316Smckusick 94*9316Smckusick 95*9316Smckusick /* 96*9316Smckusick * Initially, everything is set up as if from : 97*9316Smckusick * initstate( 1, &randtbl, 128 ); 98*9316Smckusick * Note that this initialization takes advantage of the fact that srandom() 99*9316Smckusick * advances the front and rear pointers 10*rand_deg times, and hence the 100*9316Smckusick * rear pointer which starts at 0 will also end up at zero; thus the zeroeth 101*9316Smckusick * element of the state information, which contains info about the current 102*9316Smckusick * position of the rear pointer is just 103*9316Smckusick * MAX_TYPES*(rptr - state) + TYPE_3 == TYPE_3. 104*9316Smckusick */ 105*9316Smckusick 106*9316Smckusick static long randtbl[ DEG_3 + 1 ] = { TYPE_3, 107*9316Smckusick 0x9a319039, 0x32d9c024, 0x9b663182, 0x5da1f342, 108*9316Smckusick 0xde3b81e0, 0xdf0a6fb5, 0xf103bc02, 0x48f340fb, 109*9316Smckusick 0x7449e56b, 0xbeb1dbb0, 0xab5c5918, 0x946554fd, 110*9316Smckusick 0x8c2e680f, 0xeb3d799f, 0xb11ee0b7, 0x2d436b86, 111*9316Smckusick 0xda672e2a, 0x1588ca88, 0xe369735d, 0x904f35f7, 112*9316Smckusick 0xd7158fd6, 0x6fa6f051, 0x616e6b96, 0xac94efdc, 113*9316Smckusick 0x36413f93, 0xc622c298, 0xf5a42ab8, 0x8a88d77b, 114*9316Smckusick 0xf5ad9d0e, 0x8999220b, 0x27fb47b9 }; 115*9316Smckusick 116*9316Smckusick /* 117*9316Smckusick * fptr and rptr are two pointers into the state info, a front and a rear 118*9316Smckusick * pointer. These two pointers are always rand_sep places aparts, as they cycle 119*9316Smckusick * cyclically through the state information. (Yes, this does mean we could get 120*9316Smckusick * away with just one pointer, but the code for random() is more efficient this 121*9316Smckusick * way). The pointers are left positioned as they would be from the call 122*9316Smckusick * initstate( 1, randtbl, 128 ) 123*9316Smckusick * (The position of the rear pointer, rptr, is really 0 (as explained above 124*9316Smckusick * in the initialization of randtbl) because the state table pointer is set 125*9316Smckusick * to point to randtbl[1] (as explained below). 126*9316Smckusick */ 127*9316Smckusick 128*9316Smckusick static long *fptr = &randtbl[ SEP_3 + 1 ]; 129*9316Smckusick static long *rptr = &randtbl[ 1 ]; 130*9316Smckusick 131*9316Smckusick 132*9316Smckusick 133*9316Smckusick /* 134*9316Smckusick * The following things are the pointer to the state information table, 135*9316Smckusick * the type of the current generator, the degree of the current polynomial 136*9316Smckusick * being used, and the separation between the two pointers. 137*9316Smckusick * Note that for efficiency of random(), we remember the first location of 138*9316Smckusick * the state information, not the zeroeth. Hence it is valid to access 139*9316Smckusick * state[-1], which is used to store the type of the R.N.G. 140*9316Smckusick * Also, we remember the last location, since this is more efficient than 141*9316Smckusick * indexing every time to find the address of the last element to see if 142*9316Smckusick * the front and rear pointers have wrapped. 143*9316Smckusick */ 144*9316Smckusick 145*9316Smckusick static long *state = &randtbl[ -1 ]; 146*9316Smckusick 147*9316Smckusick static int rand_type = TYPE_3; 148*9316Smckusick static int rand_deg = DEG_3; 149*9316Smckusick static int rand_sep = SEP_3; 150*9316Smckusick 151*9316Smckusick static long *end_ptr = &randtbl[ DEG_3 + 1 ]; 152*9316Smckusick 153*9316Smckusick 154*9316Smckusick 155*9316Smckusick /* 156*9316Smckusick * srandom: 157*9316Smckusick * Initialize the random number generator based on the given seed. If the 158*9316Smckusick * type is the trivial no-state-information type, just remember the seed. 159*9316Smckusick * Otherwise, initializes state[] based on the given "seed" via a linear 160*9316Smckusick * congruential generator. Then, the pointers are set to known locations 161*9316Smckusick * that are exactly rand_sep places apart. Lastly, it cycles the state 162*9316Smckusick * information a given number of times to get rid of any initial dependencies 163*9316Smckusick * introduced by the L.C.R.N.G. 164*9316Smckusick * Note that the initialization of randtbl[] for default usage relies on 165*9316Smckusick * values produced by this routine. 166*9316Smckusick */ 167*9316Smckusick 168*9316Smckusick srandom( x ) 169*9316Smckusick 170*9316Smckusick unsigned x; 171*9316Smckusick { 172*9316Smckusick register int i, j; 173*9316Smckusick 174*9316Smckusick if( rand_type == TYPE_0 ) { 175*9316Smckusick state[ 0 ] = x; 176*9316Smckusick } 177*9316Smckusick else { 178*9316Smckusick j = 1; 179*9316Smckusick state[ 0 ] = x; 180*9316Smckusick for( i = 1; i < rand_deg; i++ ) { 181*9316Smckusick state[i] = 1103515245*state[i - 1] + 12345; 182*9316Smckusick } 183*9316Smckusick fptr = &state[ rand_sep ]; 184*9316Smckusick rptr = &state[ 0 ]; 185*9316Smckusick for( i = 0; i < 10*rand_deg; i++ ) random(); 186*9316Smckusick } 187*9316Smckusick } 188*9316Smckusick 189*9316Smckusick 190*9316Smckusick 191*9316Smckusick /* 192*9316Smckusick * initstate: 193*9316Smckusick * Initialize the state information in the given array of n bytes for 194*9316Smckusick * future random number generation. Based on the number of bytes we 195*9316Smckusick * are given, and the break values for the different R.N.G.'s, we choose 196*9316Smckusick * the best (largest) one we can and set things up for it. srandom() is 197*9316Smckusick * then called to initialize the state information. 198*9316Smckusick * Note that on return from srandom(), we set state[-1] to be the type 199*9316Smckusick * multiplexed with the current value of the rear pointer; this is so 200*9316Smckusick * successive calls to initstate() won't lose this information and will 201*9316Smckusick * be able to restart with setstate(). 202*9316Smckusick * Note: the first thing we do is save the current state, if any, just like 203*9316Smckusick * setstate() so that it doesn't matter when initstate is called. 204*9316Smckusick * Returns a pointer to the old state. 205*9316Smckusick */ 206*9316Smckusick 207*9316Smckusick char * 208*9316Smckusick initstate( seed, arg_state, n ) 209*9316Smckusick 210*9316Smckusick unsigned seed; /* seed for R. N. G. */ 211*9316Smckusick char *arg_state; /* pointer to state array */ 212*9316Smckusick int n; /* # bytes of state info */ 213*9316Smckusick { 214*9316Smckusick register char *ostate = (char *)( &state[ -1 ] ); 215*9316Smckusick 216*9316Smckusick if( rand_type == TYPE_0 ) state[ -1 ] = rand_type; 217*9316Smckusick else state[ -1 ] = MAX_TYPES*(rptr - state) + rand_type; 218*9316Smckusick if( n < BREAK_1 ) { 219*9316Smckusick if( n < BREAK_0 ) { 220*9316Smckusick fprintf( stderr, "initstate: not enough state (%d bytes) with which to do jack; ignored.\n" ); 221*9316Smckusick return; 222*9316Smckusick } 223*9316Smckusick rand_type = TYPE_0; 224*9316Smckusick rand_deg = DEG_0; 225*9316Smckusick rand_sep = SEP_0; 226*9316Smckusick } 227*9316Smckusick else { 228*9316Smckusick if( n < BREAK_2 ) { 229*9316Smckusick rand_type = TYPE_1; 230*9316Smckusick rand_deg = DEG_1; 231*9316Smckusick rand_sep = SEP_1; 232*9316Smckusick } 233*9316Smckusick else { 234*9316Smckusick if( n < BREAK_3 ) { 235*9316Smckusick rand_type = TYPE_2; 236*9316Smckusick rand_deg = DEG_2; 237*9316Smckusick rand_sep = SEP_2; 238*9316Smckusick } 239*9316Smckusick else { 240*9316Smckusick if( n < BREAK_4 ) { 241*9316Smckusick rand_type = TYPE_3; 242*9316Smckusick rand_deg = DEG_3; 243*9316Smckusick rand_sep = SEP_3; 244*9316Smckusick } 245*9316Smckusick else { 246*9316Smckusick rand_type = TYPE_4; 247*9316Smckusick rand_deg = DEG_4; 248*9316Smckusick rand_sep = SEP_4; 249*9316Smckusick } 250*9316Smckusick } 251*9316Smckusick } 252*9316Smckusick } 253*9316Smckusick state = &( ( (long *)arg_state )[1] ); /* first location */ 254*9316Smckusick end_ptr = &state[ rand_deg ]; /* must set end_ptr before srandom */ 255*9316Smckusick srandom( seed ); 256*9316Smckusick if( rand_type == TYPE_0 ) state[ -1 ] = rand_type; 257*9316Smckusick else state[ -1 ] = MAX_TYPES*(rptr - state) + rand_type; 258*9316Smckusick return( ostate ); 259*9316Smckusick } 260*9316Smckusick 261*9316Smckusick 262*9316Smckusick 263*9316Smckusick /* 264*9316Smckusick * setstate: 265*9316Smckusick * Restore the state from the given state array. 266*9316Smckusick * Note: it is important that we also remember the locations of the pointers 267*9316Smckusick * in the current state information, and restore the locations of the pointers 268*9316Smckusick * from the old state information. This is done by multiplexing the pointer 269*9316Smckusick * location into the zeroeth word of the state information. 270*9316Smckusick * Note that due to the order in which things are done, it is OK to call 271*9316Smckusick * setstate() with the same state as the current state. 272*9316Smckusick * Returns a pointer to the old state information. 273*9316Smckusick */ 274*9316Smckusick 275*9316Smckusick char * 276*9316Smckusick setstate( arg_state ) 277*9316Smckusick 278*9316Smckusick char *arg_state; 279*9316Smckusick { 280*9316Smckusick register long *new_state = (long *)arg_state; 281*9316Smckusick register int type = new_state[0]%MAX_TYPES; 282*9316Smckusick register int rear = new_state[0]/MAX_TYPES; 283*9316Smckusick char *ostate = (char *)( &state[ -1 ] ); 284*9316Smckusick 285*9316Smckusick if( rand_type == TYPE_0 ) state[ -1 ] = rand_type; 286*9316Smckusick else state[ -1 ] = MAX_TYPES*(rptr - state) + rand_type; 287*9316Smckusick switch( type ) { 288*9316Smckusick case TYPE_0: 289*9316Smckusick case TYPE_1: 290*9316Smckusick case TYPE_2: 291*9316Smckusick case TYPE_3: 292*9316Smckusick case TYPE_4: 293*9316Smckusick rand_type = type; 294*9316Smckusick rand_deg = degrees[ type ]; 295*9316Smckusick rand_sep = seps[ type ]; 296*9316Smckusick break; 297*9316Smckusick 298*9316Smckusick default: 299*9316Smckusick fprintf( stderr, "setstate: state info has been munged; not changed.\n" ); 300*9316Smckusick } 301*9316Smckusick state = &new_state[ 1 ]; 302*9316Smckusick if( rand_type != TYPE_0 ) { 303*9316Smckusick rptr = &state[ rear ]; 304*9316Smckusick fptr = &state[ (rear + rand_sep)%rand_deg ]; 305*9316Smckusick } 306*9316Smckusick end_ptr = &state[ rand_deg ]; /* set end_ptr too */ 307*9316Smckusick return( ostate ); 308*9316Smckusick } 309*9316Smckusick 310*9316Smckusick 311*9316Smckusick 312*9316Smckusick /* 313*9316Smckusick * random: 314*9316Smckusick * If we are using the trivial TYPE_0 R.N.G., just do the old linear 315*9316Smckusick * congruential bit. Otherwise, we do our fancy trinomial stuff, which is the 316*9316Smckusick * same in all ther other cases due to all the global variables that have been 317*9316Smckusick * set up. The basic operation is to add the number at the rear pointer into 318*9316Smckusick * the one at the front pointer. Then both pointers are advanced to the next 319*9316Smckusick * location cyclically in the table. The value returned is the sum generated, 320*9316Smckusick * reduced to 31 bits by throwing away the "least random" low bit. 321*9316Smckusick * Note: the code takes advantage of the fact that both the front and 322*9316Smckusick * rear pointers can't wrap on the same call by not testing the rear 323*9316Smckusick * pointer if the front one has wrapped. 324*9316Smckusick * Returns a 31-bit random number. 325*9316Smckusick */ 326*9316Smckusick 327*9316Smckusick long 328*9316Smckusick random() 329*9316Smckusick { 330*9316Smckusick long i; 331*9316Smckusick 332*9316Smckusick if( rand_type == TYPE_0 ) { 333*9316Smckusick i = state[0] = ( state[0]*1103515245 + 12345 )&0x7fffffff; 334*9316Smckusick } 335*9316Smckusick else { 336*9316Smckusick *fptr += *rptr; 337*9316Smckusick i = (*fptr >> 1)&0x7fffffff; /* chucking least random bit */ 338*9316Smckusick if( ++fptr >= end_ptr ) { 339*9316Smckusick fptr = state; 340*9316Smckusick ++rptr; 341*9316Smckusick } 342*9316Smckusick else { 343*9316Smckusick if( ++rptr >= end_ptr ) rptr = state; 344*9316Smckusick } 345*9316Smckusick } 346*9316Smckusick return( i ); 347*9316Smckusick } 348*9316Smckusick 349