1*32689Sbostic #ifndef lint 2*32689Sbostic static char sccsid[] = "@(#)random.c 5.1 (Berkeley) 11/25/87"; 3*32689Sbostic #endif /* not lint */ 4*32689Sbostic 5*32689Sbostic static long rntb[32] = { 6*32689Sbostic 3, 0x9a319039, 0x32d9c024, 0x9b663182, 0x5da1f342, 7*32689Sbostic 0xde3b81e0, 0xdf0a6fb5, 0xf103bc02, 0x48f340fb, 0x7449e56b, 8*32689Sbostic 0xbeb1dbb0, 0xab5c5918, 0x946554fd, 0x8c2e680f, 0xeb3d799f, 9*32689Sbostic 0xb11ee0b7, 0x2d436b86, 0xda672e2a, 0x1588ca88, 0xe369735d, 10*32689Sbostic 0x904f35f7, 0xd7158fd6, 0x6fa6f051, 0x616e6b96, 0xac94efdc, 11*32689Sbostic 0x36413f93, 0xc622c298, 0xf5a42ab8, 0x8a88d77b, 0xf5ad9d0e, 12*32689Sbostic 0x8999220b, 0x27fb47b9 13*32689Sbostic }; 14*32689Sbostic 15*32689Sbostic static long *fptr = &rntb[4]; 16*32689Sbostic static long *rptr = &rntb[1]; 17*32689Sbostic static long *state = &rntb[1]; 18*32689Sbostic static int rand_type = 3; 19*32689Sbostic static int rand_deg = 31; 20*32689Sbostic static int rand_sep = 3; 21*32689Sbostic static long *end_ptr = &rntb[32]; 22*32689Sbostic 23*32689Sbostic srrandom(x) 24*32689Sbostic int x; 25*32689Sbostic { 26*32689Sbostic register int i; 27*32689Sbostic long rrandom(); 28*32689Sbostic 29*32689Sbostic state[0] = (long) x; 30*32689Sbostic if (rand_type != 0) { 31*32689Sbostic for (i = 1; i < rand_deg; i++) { 32*32689Sbostic state[i] = 1103515245 * state[i - 1] + 12345; 33*32689Sbostic } 34*32689Sbostic fptr = &state[rand_sep]; 35*32689Sbostic rptr = &state[0]; 36*32689Sbostic for (i = 0; i < 10 * rand_deg; i++) { 37*32689Sbostic (void) rrandom(); 38*32689Sbostic } 39*32689Sbostic } 40*32689Sbostic } 41*32689Sbostic 42*32689Sbostic long 43*32689Sbostic rrandom() 44*32689Sbostic { 45*32689Sbostic long i; 46*32689Sbostic 47*32689Sbostic if (rand_type == 0) { 48*32689Sbostic i = state[0] = (state[0]*1103515245 + 12345) & 0x7fffffff; 49*32689Sbostic } else { 50*32689Sbostic *fptr += *rptr; 51*32689Sbostic i = (*fptr >> 1) & 0x7fffffff; 52*32689Sbostic if (++fptr >= end_ptr) { 53*32689Sbostic fptr = state; 54*32689Sbostic ++rptr; 55*32689Sbostic } else { 56*32689Sbostic if (++rptr >= end_ptr) { 57*32689Sbostic rptr = state; 58*32689Sbostic } 59*32689Sbostic } 60*32689Sbostic } 61*32689Sbostic return(i); 62*32689Sbostic } 63*32689Sbostic 64*32689Sbostic get_rand(x, y) 65*32689Sbostic register int x, y; 66*32689Sbostic { 67*32689Sbostic register int r, t; 68*32689Sbostic long lr; 69*32689Sbostic 70*32689Sbostic if (x > y) { 71*32689Sbostic t = y; 72*32689Sbostic y = x; 73*32689Sbostic x = t; 74*32689Sbostic } 75*32689Sbostic lr = rrandom(); 76*32689Sbostic lr &= (long) 0x00003fff; 77*32689Sbostic r = (int) lr; 78*32689Sbostic r = (r % ((y - x) + 1)) + x; 79*32689Sbostic return(r); 80*32689Sbostic } 81*32689Sbostic 82*32689Sbostic rand_percent(percentage) 83*32689Sbostic register int percentage; 84*32689Sbostic { 85*32689Sbostic return(get_rand(1, 100) <= percentage); 86*32689Sbostic } 87*32689Sbostic 88*32689Sbostic coin_toss() 89*32689Sbostic { 90*32689Sbostic 91*32689Sbostic return(((rrandom() & 01) ? 1 : 0)); 92*32689Sbostic } 93