Overview
| Artifact ID: | 8908e7a353e17ce3d891606e45005d9cc484cf2e |
|---|---|
| Ticket: | 221072ffffffffffffffffffffffffffffffffff
rand() > 1 on 64-bit platform |
| User & Date: | dgp on 2000-11-09 02:15:24 |
Changes
- icomment:
OK, here's the patch incorporating all the comments and corrections I've seen: Index: generic/tclExecute.c =================================================================== RCS file: /home/cvs/cvsroot/sun/tcl/generic/tclExecute.c,v retrieving revision 1.1.1.13 retrieving revision 1.9 diff -c -r1.1.1.13 -r1.9 *** tclExecute.c2000/11/06 16:26:531.1.1.13 --- tclExecute.c2000/11/06 19:34:191.9 *************** *** 4042,4052 **** register int stackTop;/* Cached top index of evaluation stack. */ Interp *iPtr = (Interp *) interp; double dResult; ! int tmp; if (!(iPtr->flags & RAND_SEED_INITIALIZED)) { iPtr->flags |= RAND_SEED_INITIALIZED; iPtr->randSeed = TclpGetClicks(); } /* --- 4042,4062 ---- register int stackTop;/* Cached top index of evaluation stack. */ Interp *iPtr = (Interp *) interp; double dResult; ! long tmp;/* Algorithm assumes at least 32 bits. ! * Only long guarantees that. See below. */ if (!(iPtr->flags & RAND_SEED_INITIALIZED)) { iPtr->flags |= RAND_SEED_INITIALIZED; iPtr->randSeed = TclpGetClicks(); + + /* + * Make sure 1 <= randSeed <= (2^31) - 2. See below. + */ + + iPtr->randSeed &= (unsigned long) 0x7fffffff; + if ((iPtr->randSeed == 0) || (iPtr->randSeed == 0x7fffffff)) { + iPtr->randSeed ^= 123459876; + } } /* *************** *** 4059,4069 **** * Generate the random number using the linear congruential * generator defined by the following recurrence: *seed = ( IA * seed ) mod IM ! * where IA is 16807 and IM is (2^31) - 1. In order to avoid ! * potential problems with integer overflow, the code uses ! * additional constants IQ and IR such that *IM = IA*IQ + IR ! * For details on how this algorithm works, refer to the following * papers: * *S.K. Park & K.W. Miller, "Random number generators: good ones --- 4069,4088 ---- * Generate the random number using the linear congruential * generator defined by the following recurrence: *seed = ( IA * seed ) mod IM ! * where IA is 16807 and IM is (2^31) - 1. The recurrence maps ! * a seed in the range [1, IM - 1] to a new seed in that same range. ! * The recurrence maps IM to 0, and maps 0 back to 0, so those two ! * values must not be allowed as initial values of seed. ! * ! * In order to avoid potential problems with integer overflow, the ! * recurrence is implemented in terms of additional constants ! * IQ and IR such that *IM = IA*IQ + IR ! * None of the operations in the implementation overflows a 32-bit ! * signed integer, and the C type long is guaranteed to be at least ! * 32 bits wide. ! * ! * For more details on how this algorithm works, refer to the following * papers: * *S.K. Park & K.W. Miller, "Random number generators: good ones *************** *** 4079,4092 **** #define RAND_IR2836 #define RAND_MASK123459876 - if (iPtr->randSeed == 0) { - /* - * Don't allow a 0 seed, since it breaks the generator. Shift - * it to some other value. - */ - - iPtr->randSeed = 123459876; - } tmp = iPtr->randSeed/RAND_IQ; iPtr->randSeed = RAND_IA*(iPtr->randSeed - tmp*RAND_IQ) - RAND_IR*tmp; if (iPtr->randSeed < 0) { --- 4098,4103 ---- *************** *** 4094,4107 **** } /* ! * On 64-bit architectures we need to mask off the upper bits to ! * ensure we only have a 32-bit range. The constant has the ! * bizarre form below in order to make sure that it doesn't ! * get sign-extended (the rules for sign extension are very ! * concat, particularly on 64-bit machines). */ - iPtr->randSeed &= ((((unsigned long) 0xfffffff) << 4) | 0xf); dResult = iPtr->randSeed * (1.0/RAND_IM); /* --- 4105,4114 ---- } /* ! * Since the recurrence keeps seed values in the range [1, RAND_IM - 1], ! * dividing by RAND_IM yields a double in the range (0, 1). */ dResult = iPtr->randSeed * (1.0/RAND_IM); /* *************** *** 4248,4258 **** } /* ! * Reset the seed. */ iPtr->flags |= RAND_SEED_INITIALIZED; iPtr->randSeed = i; /* * To avoid duplicating the random number generation code we simply --- 4255,4270 ---- } /* ! * Reset the seed. Make sure 1 <= randSeed <= 2^31 - 2. ! * See comments in ExprRandFunc() for more details. */ iPtr->flags |= RAND_SEED_INITIALIZED; iPtr->randSeed = i; + iPtr->randSeed &= (unsigned long) 0x7fffffff; + if ((iPtr->randSeed == 0) || (iPtr->randSeed == 0x7fffffff)) { + iPtr->randSeed ^= 123459876; + } /* * To avoid duplicating the random number generation code we simply Index: tests/expr-old.test =================================================================== RCS file: /home/cvs/cvsroot/sun/tcl/tests/expr-old.test,v retrieving revision 1.1.1.12 retrieving revision 1.8 diff -c -r1.1.1.12 -r1.8 *** expr-old.test2000/06/07 14:41:531.1.1.12 --- expr-old.test2000/11/06 19:34:191.8 *************** *** 825,830 **** --- 825,836 ---- test expr-old-32.51 {math functions in expressions} { list [catch {expr {srand([lindex "6ty" 0])}} msg] $msg } {1 {argument to math function didn't have numeric value}} + test expr-old-32.52 {math functions in expressions} { + expr {srand(1<<37) < 1} + } {1} + test expr-old-32.53 {math functions in expressions} { + expr {srand((1<<31) - 1) > 0} + } {1} test expr-old-33.1 {conversions and fancy args to math functions} { expr hypot ( 3 , 4 ) - login: "dgp"
- mimetype: "text/plain"
- username: "dgp"