diff options
Diffstat (limited to 'src/lib/libc/stdlib/random.c')
-rw-r--r-- | src/lib/libc/stdlib/random.c | 48 |
1 files changed, 27 insertions, 21 deletions
diff --git a/src/lib/libc/stdlib/random.c b/src/lib/libc/stdlib/random.c index 469b6d976a..79344f30f1 100644 --- a/src/lib/libc/stdlib/random.c +++ b/src/lib/libc/stdlib/random.c | |||
@@ -32,8 +32,7 @@ | |||
32 | */ | 32 | */ |
33 | 33 | ||
34 | #if defined(LIBC_SCCS) && !defined(lint) | 34 | #if defined(LIBC_SCCS) && !defined(lint) |
35 | /*static char *sccsid = "from: @(#)random.c 5.9 (Berkeley) 2/23/91";*/ | 35 | static char *rcsid = "$OpenBSD: random.c,v 1.6 1998/02/07 02:16:25 millert Exp $"; |
36 | static char *rcsid = "$Id: random.c,v 1.1.1.1 1995/10/18 08:42:19 deraadt Exp $"; | ||
37 | #endif /* LIBC_SCCS and not lint */ | 36 | #endif /* LIBC_SCCS and not lint */ |
38 | 37 | ||
39 | #include <stdio.h> | 38 | #include <stdio.h> |
@@ -136,12 +135,12 @@ static int seps [MAX_TYPES] = { SEP_0, SEP_1, SEP_2, SEP_3, SEP_4 }; | |||
136 | 135 | ||
137 | static long randtbl[DEG_3 + 1] = { | 136 | static long randtbl[DEG_3 + 1] = { |
138 | TYPE_3, | 137 | TYPE_3, |
139 | 0x9a319039, 0x32d9c024, 0x9b663182, 0x5da1f342, 0xde3b81e0, 0xdf0a6fb5, | 138 | 0x991539b1, 0x16a5bce3, 0x6774a4cd, 0x3e01511e, 0x4e508aaa, 0x61048c05, |
140 | 0xf103bc02, 0x48f340fb, 0x7449e56b, 0xbeb1dbb0, 0xab5c5918, 0x946554fd, | 139 | 0xf5500617, 0x846b7115, 0x6a19892c, 0x896a97af, 0xdb48f936, 0x14898454, |
141 | 0x8c2e680f, 0xeb3d799f, 0xb11ee0b7, 0x2d436b86, 0xda672e2a, 0x1588ca88, | 140 | 0x37ffd106, 0xb58bff9c, 0x59e17104, 0xcf918a49, 0x09378c83, 0x52c7a471, |
142 | 0xe369735d, 0x904f35f7, 0xd7158fd6, 0x6fa6f051, 0x616e6b96, 0xac94efdc, | 141 | 0x8d293ea9, 0x1f4fc301, 0xc3db71be, 0x39b44e1c, 0xf8a44ef9, 0x4c8b80b1, |
143 | 0x36413f93, 0xc622c298, 0xf5a42ab8, 0x8a88d77b, 0xf5ad9d0e, 0x8999220b, | 142 | 0x19edc328, 0x87bf4bdd, 0xc9b240e5, 0xe9ee4b1b, 0x4382aee7, 0x535b6b41, |
144 | 0x27fb47b9, | 143 | 0xf3bec5da, |
145 | }; | 144 | }; |
146 | 145 | ||
147 | /* | 146 | /* |
@@ -193,15 +192,26 @@ void | |||
193 | srandom(x) | 192 | srandom(x) |
194 | u_int x; | 193 | u_int x; |
195 | { | 194 | { |
196 | register int i, j; | 195 | register long int test; |
196 | register int i; | ||
197 | ldiv_t val; | ||
197 | 198 | ||
198 | if (rand_type == TYPE_0) | 199 | if (rand_type == TYPE_0) |
199 | state[0] = x; | 200 | state[0] = x; |
200 | else { | 201 | else { |
201 | j = 1; | ||
202 | state[0] = x; | 202 | state[0] = x; |
203 | for (i = 1; i < rand_deg; i++) | 203 | for (i = 1; i < rand_deg; i++) { |
204 | state[i] = 1103515245 * state[i - 1] + 12345; | 204 | /* |
205 | * Implement the following, without overflowing 31 bits: | ||
206 | * | ||
207 | * state[i] = (16807 * state[i - 1]) % 2147483647; | ||
208 | * | ||
209 | * 2^31-1 (prime) = 2147483647 = 127773*16807+2836 | ||
210 | */ | ||
211 | val = ldiv(state[i-1], 127773); | ||
212 | test = 16807 * val.rem - 2836 * val.quot; | ||
213 | state[i] = test + (test < 0 ? 2147483647 : 0); | ||
214 | } | ||
205 | fptr = &state[rand_sep]; | 215 | fptr = &state[rand_sep]; |
206 | rptr = &state[0]; | 216 | rptr = &state[0]; |
207 | for (i = 0; i < 10 * rand_deg; i++) | 217 | for (i = 0; i < 10 * rand_deg; i++) |
@@ -232,7 +242,7 @@ char * | |||
232 | initstate(seed, arg_state, n) | 242 | initstate(seed, arg_state, n) |
233 | u_int seed; /* seed for R.N.G. */ | 243 | u_int seed; /* seed for R.N.G. */ |
234 | char *arg_state; /* pointer to state array */ | 244 | char *arg_state; /* pointer to state array */ |
235 | int n; /* # bytes of state info */ | 245 | size_t n; /* # bytes of state info */ |
236 | { | 246 | { |
237 | register char *ostate = (char *)(&state[-1]); | 247 | register char *ostate = (char *)(&state[-1]); |
238 | 248 | ||
@@ -240,11 +250,8 @@ initstate(seed, arg_state, n) | |||
240 | state[-1] = rand_type; | 250 | state[-1] = rand_type; |
241 | else | 251 | else |
242 | state[-1] = MAX_TYPES * (rptr - state) + rand_type; | 252 | state[-1] = MAX_TYPES * (rptr - state) + rand_type; |
243 | if (n < BREAK_0) { | 253 | if (n < BREAK_0) |
244 | (void)fprintf(stderr, | 254 | return(NULL); |
245 | "random: not enough state (%d bytes); ignored.\n", n); | ||
246 | return(0); | ||
247 | } | ||
248 | if (n < BREAK_1) { | 255 | if (n < BREAK_1) { |
249 | rand_type = TYPE_0; | 256 | rand_type = TYPE_0; |
250 | rand_deg = DEG_0; | 257 | rand_deg = DEG_0; |
@@ -293,7 +300,7 @@ initstate(seed, arg_state, n) | |||
293 | */ | 300 | */ |
294 | char * | 301 | char * |
295 | setstate(arg_state) | 302 | setstate(arg_state) |
296 | char *arg_state; | 303 | const char *arg_state; |
297 | { | 304 | { |
298 | register long *new_state = (long *)arg_state; | 305 | register long *new_state = (long *)arg_state; |
299 | register int type = new_state[0] % MAX_TYPES; | 306 | register int type = new_state[0] % MAX_TYPES; |
@@ -315,8 +322,7 @@ setstate(arg_state) | |||
315 | rand_sep = seps[type]; | 322 | rand_sep = seps[type]; |
316 | break; | 323 | break; |
317 | default: | 324 | default: |
318 | (void)fprintf(stderr, | 325 | return(NULL); |
319 | "random: state info corrupted; not changed.\n"); | ||
320 | } | 326 | } |
321 | state = &new_state[1]; | 327 | state = &new_state[1]; |
322 | if (rand_type != TYPE_0) { | 328 | if (rand_type != TYPE_0) { |