On Sun, Feb 02, 2003 at 13:26:21 +0300, Andrey A. Chernov wrote: > Workaround I find so far is something like that > > #define MASK 123459876
I found nothing better. Here is fix for 0 problem I plan to commit: --- stdlib/rand.c.old Sat Jan 4 20:39:19 2003 +++ stdlib/rand.c Sun Feb 2 14:43:42 2003 @@ -70,14 +70,18 @@ * Park and Miller, Communications of the ACM, vol. 31, no. 10, * October 1988, p. 1195. */ +#define SHIFT_MASK 123459876 long hi, lo, x; - hi = *ctx / 127773; - lo = *ctx % 127773; + /* Can't be initialized with 0, so use shifting mask. */ + x = *ctx ^ SHIFT_MASK; + hi = x / 127773; + lo = x % 127773; x = 16807 * lo - 2836 * hi; - if (x <= 0) + if (x < 0) x += 0x7fffffff; - return ((*ctx = x) % ((u_long)RAND_MAX + 1)); + *ctx = x ^ SHIFT_MASK; + return (x % ((u_long)RAND_MAX + 1)); #endif /* !USE_WEAK_SEEDING */ } @@ -86,8 +90,10 @@ rand_r(unsigned int *ctx) { u_long val = (u_long) *ctx; - *ctx = do_rand(&val); - return (int) *ctx; + int r = do_rand(&val); + + *ctx = (unsigned int) val; + return (r); } --- stdlib/random.c.old Sun Mar 24 23:42:48 2002 +++ stdlib/random.c Sun Feb 2 15:24:38 2003 @@ -142,6 +142,10 @@ */ #define MAX_TYPES 5 /* max number of types above */ +#ifndef USE_WEAK_SEEDING +#define SHIFT_MASK 123459876 +#endif + static long degrees[MAX_TYPES] = { DEG_0, DEG_1, DEG_2, DEG_3, DEG_4 }; static long seps [MAX_TYPES] = { SEP_0, SEP_1, SEP_2, SEP_3, SEP_4 }; @@ -171,12 +175,12 @@ 0x36413f93, 0xc622c298, 0xf5a42ab8, 0x8a88d77b, 0xf5ad9d0e, 0x8999220b, 0x27fb47b9, #else /* !USE_WEAK_SEEDING */ - 0x991539b1, 0x16a5bce3, 0x6774a4cd, 0x3e01511e, 0x4e508aaa, 0x61048c05, - 0xf5500617, 0x846b7115, 0x6a19892c, 0x896a97af, 0xdb48f936, 0x14898454, - 0x37ffd106, 0xb58bff9c, 0x59e17104, 0xcf918a49, 0x09378c83, 0x52c7a471, - 0x8d293ea9, 0x1f4fc301, 0xc3db71be, 0x39b44e1c, 0xf8a44ef9, 0x4c8b80b1, - 0x19edc328, 0x87bf4bdd, 0xc9b240e5, 0xe9ee4b1b, 0x4382aee7, 0x535b6b41, - 0xf3bec5da + 0x52a59789, 0x43164b1c, 0x7be52a82, 0x748ef343, 0x642a8923, 0x6ade1cd8, + 0x1ae76e27, 0x24b915ee, 0x2c42f326, 0x12ab3ee1, 0x4679af03, 0x876d19a0, + 0xe9e535ba, 0xad2471c8, 0x710262f8, 0xe1c16494, 0x29224bcc, 0x9710c348, + 0x7347f8e4, 0xe01ef1b4, 0x2030c33f, 0xd465e38, 0x925375aa, 0x6091d15d, + 0x467ee7d7, 0x92713312, 0x32346127, 0x8350e834, 0x3dadc6ea, 0x391364f2, + 0x8226561c, #endif /* !USE_WEAK_SEEDING */ }; @@ -236,12 +240,14 @@ */ long hi, lo; + /* Can't be initialized with 0, so use shifting mask. */ + x ^= SHIFT_MASK; hi = x / 127773; lo = x % 127773; x = 16807 * lo - 2836 * hi; - if (x <= 0) + if (x < 0) x += 0x7fffffff; - return (x); + return (x ^ SHIFT_MASK); /* return state, not value */ #endif /* !USE_WEAK_SEEDING */ } @@ -473,7 +479,12 @@ if (rand_type == TYPE_0) { i = state[0]; - state[0] = i = (good_rand(i)) & 0x7fffffff; + state[0] = good_rand(i); +#ifndef USE_WEAK_SEEDING + i = state[0] ^ SHIFT_MASK; +#else + i = state[0] & 0x7fffffff; +#endif } else { /* * Use local variables rather than static variables for speed. -- Andrey A. Chernov http://ache.pp.ru/
msg51505/pgp00000.pgp
Description: PGP signature