diff options
Diffstat (limited to 'stdlib/random.c')
-rw-r--r-- | stdlib/random.c | 26 |
1 files changed, 18 insertions, 8 deletions
diff --git a/stdlib/random.c b/stdlib/random.c index fb32b36b87..473a5b13d3 100644 --- a/stdlib/random.c +++ b/stdlib/random.c @@ -122,12 +122,14 @@ static int seps[MAX_TYPES] = { SEP_0, SEP_1, SEP_2, SEP_3, SEP_4 }; static long int randtbl[DEG_3 + 1] = { TYPE_3, - -851904987, -43806228, -2029755270, 1390239686, -1912102820, - -485608943, 1969813258, -1590463333, -1944053249, 455935928, 508023712, - -1714531963, 1800685987, -2015299881, 654595283, -1149023258, - -1470005550, -1143256056, -1325577603, -1568001885, 1275120390, - -607508183, -205999574, -1696891592, 1492211999, -1528267240, - -952028296, -189082757, 362343714, 1424981831, 2039449641, + + -1726662223, 379960547, 1735697613, 1040273694, 1313901226, + 1627687941, -179304937, -2073333483, 1780058412, -1989503057, + -615974602, 344556628, 939512070, -1249116260, 1507946756, + -812545463, 154635395, 1388815473, -1926676823, 525320961, + -1009028674, 968117788, -123449607, 1284210865, 435012392, + -2017506339, -911064859, -370259173, 1132637927, 1398500161, + -205601318, }; /* FPTR and RPTR are two pointers into the state info, a front and a rear @@ -179,11 +181,19 @@ DEFUN(__srandom, (x), unsigned int x) { register long int i; for (i = 1; i < rand_deg; ++i) - state[i] = (1103515145 * state[i - 1]) + 12345; + { + /* This does: + state[i] = (16807 * state[i - 1]) % 2147483647; + but avoids overflowing 31 bits. */ + long int hi = state[i - 1] / 127773; + long int lo = state[i - 1] % 127773; + long int test = 16807 * lo - 2836 * hi; + state[i] = test + (test < 0 ? 2147483647 : 0); + } fptr = &state[rand_sep]; rptr = &state[0]; for (i = 0; i < 10 * rand_deg; ++i) - (void) __random(); + (void) __random (); } } |