1 /* 2 * Copyright (c) 1988, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * This code is derived from software contributed to Berkeley by 6 * Timothy C. Stoehr. 7 * 8 * %sccs.include.redist.c% 9 */ 10 11 #ifndef lint 12 static char sccsid[] = "@(#)random.c 8.1 (Berkeley) 05/31/93"; 13 #endif /* not lint */ 14 15 /* 16 * random.c 17 * 18 * This source herein may be modified and/or distributed by anybody who 19 * so desires, with the following restrictions: 20 * 1.) No portion of this notice shall be removed. 21 * 2.) Credit shall not be taken for the creation of this source. 22 * 3.) This code is not to be traded, sold, or used for personal 23 * gain or profit. 24 * 25 */ 26 27 static long rntb[32] = { 28 3, 0x9a319039, 0x32d9c024, 0x9b663182, 0x5da1f342, 29 0xde3b81e0, 0xdf0a6fb5, 0xf103bc02, 0x48f340fb, 0x7449e56b, 30 0xbeb1dbb0, 0xab5c5918, 0x946554fd, 0x8c2e680f, 0xeb3d799f, 31 0xb11ee0b7, 0x2d436b86, 0xda672e2a, 0x1588ca88, 0xe369735d, 32 0x904f35f7, 0xd7158fd6, 0x6fa6f051, 0x616e6b96, 0xac94efdc, 33 0x36413f93, 0xc622c298, 0xf5a42ab8, 0x8a88d77b, 0xf5ad9d0e, 34 0x8999220b, 0x27fb47b9 35 }; 36 37 static long *fptr = &rntb[4]; 38 static long *rptr = &rntb[1]; 39 static long *state = &rntb[1]; 40 static int rand_type = 3; 41 static int rand_deg = 31; 42 static int rand_sep = 3; 43 static long *end_ptr = &rntb[32]; 44 45 srrandom(x) 46 int x; 47 { 48 register int i; 49 long rrandom(); 50 51 state[0] = (long) x; 52 if (rand_type != 0) { 53 for (i = 1; i < rand_deg; i++) { 54 state[i] = 1103515245 * state[i - 1] + 12345; 55 } 56 fptr = &state[rand_sep]; 57 rptr = &state[0]; 58 for (i = 0; i < 10 * rand_deg; i++) { 59 (void) rrandom(); 60 } 61 } 62 } 63 64 long 65 rrandom() 66 { 67 long i; 68 69 if (rand_type == 0) { 70 i = state[0] = (state[0]*1103515245 + 12345) & 0x7fffffff; 71 } else { 72 *fptr += *rptr; 73 i = (*fptr >> 1) & 0x7fffffff; 74 if (++fptr >= end_ptr) { 75 fptr = state; 76 ++rptr; 77 } else { 78 if (++rptr >= end_ptr) { 79 rptr = state; 80 } 81 } 82 } 83 return(i); 84 } 85 86 get_rand(x, y) 87 register int x, y; 88 { 89 register int r, t; 90 long lr; 91 92 if (x > y) { 93 t = y; 94 y = x; 95 x = t; 96 } 97 lr = rrandom(); 98 lr &= (long) 0x00003fff; 99 r = (int) lr; 100 r = (r % ((y - x) + 1)) + x; 101 return(r); 102 } 103 104 rand_percent(percentage) 105 register int percentage; 106 { 107 return(get_rand(1, 100) <= percentage); 108 } 109 110 coin_toss() 111 { 112 113 return(((rrandom() & 01) ? 1 : 0)); 114 } 115