Skip to content
Tcl Source Code Artifact [e8538f653c]
Login

Artifact [e8538f653c]

Artifact e8538f653c37b37ef115f8f0a0e9cf068499ab2a:

Ticket change [e8538f653c] - Ticket [221072ffffffffff|221072ffff] <i>rand() &gt; 1 on 64-bit platform</i> status still Open with 4 other changes by dgp on 2000-11-03 13:54:17.
D 2000-11-03T13:54:17
J icomment OK,\safter\sa\strip\sto\slibrary\sto\sfetch\sthe\ssecond\nreferenced\spaper,\shere's\sa\sdifferent\spatch\sthat\nfixes\sthe\sbug,\sbut\salso\simplements\sthe\salgorithm\nin\sa\smore\sstraightforward\sportable\sway,\swith\scomments\nexplaining\sthe\simportant\sdetails.\s\s\n\n***\sgeneric/tclExecute.c.orig\s\s\sFri\sNov\s\s3\s00:30:30\s2000\n---\sgeneric/tclExecute.c\s\s\s\s\s\s\s\sFri\sNov\s\s3\s01:34:02\s2000\n***************\n***\s4042,4052\s****\n\s\s\s\s\s\sregister\sint\sstackTop;\s\s\s\s/*\sCached\stop\sindex\sof\sevaluation\sstack.\s*/\n\s\s\s\s\s\sInterp\s*iPtr\s=\s(Interp\s*)\sinterp;\n\s\s\s\s\s\sdouble\sdResult;\n!\s\s\s\s\sint\stmp;\n\s\s\n\s\s\s\s\s\sif\s(!(iPtr->flags\s&\sRAND_SEED_INITIALIZED))\s{\n\s\s\s\s\s\s\s\siPtr->flags\s|=\sRAND_SEED_INITIALIZED;\n\s\s\s\s\s\s\s\siPtr->randSeed\s=\sTclpGetClicks();\n\s\s\s\s\s\s}\n\s\s\s\s\s\s\n\s\s\s\s\s\s/*\n---\s4042,4060\s----\n\s\s\s\s\s\sregister\sint\sstackTop;\s\s\s\s/*\sCached\stop\sindex\sof\sevaluation\sstack.\s*/\n\s\s\s\s\s\sInterp\s*iPtr\s=\s(Interp\s*)\sinterp;\n\s\s\s\s\s\sdouble\sdResult;\n!\s\s\s\s\slong\stmp;\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s/*\sAlgorithm\sassumes\sat\sleast\s32\sbits.\n!\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s*\sOnly\slong\sguarantees\sthat.\s\sSee\sbelow.\s*/\n\s\s\n\s\s\s\s\s\sif\s(!(iPtr->flags\s&\sRAND_SEED_INITIALIZED))\s{\n\s\s\s\s\s\s\s\siPtr->flags\s|=\sRAND_SEED_INITIALIZED;\n\s\s\s\s\s\s\s\siPtr->randSeed\s=\sTclpGetClicks();\n+\s\n+\s\s\s\s\s\s\s/*\n+\s\s\s\s\s\s\s\s*\sMake\ssure\s1\s<=\srandSeed\s<=\s(2^31)\s-\s2.\s\sSee\sbelow.\n+\s\s\s\s\s\s\s\s*/\n+\s\n+\s\s\s\s\s\s\s\s\siPtr->randSeed\s&=\s(unsigned\slong)\s0x7fffffff;\n+\s\s\s\s\s\s\siPtr->randSeed\s^=\s123459876;\n\s\s\s\s\s\s}\n\s\s\s\s\s\s\n\s\s\s\s\s\s/*\n***************\n***\s4059,4069\s****\n\s\s\s\s\s\s\s*\sGenerate\sthe\srandom\snumber\susing\sthe\slinear\scongruential\n\s\s\s\s\s\s\s*\sgenerator\sdefined\sby\sthe\sfollowing\srecurrence:\n\s\s\s\s\s\s\s*\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s\sseed\s=\s(\sIA\s*\sseed\s)\smod\sIM\n!\s\s\s\s\s\s*\swhere\sIA\sis\s16807\sand\sIM\sis\s(2^31)\s-\s1.\s\sIn\sorder\sto\savoid\n!\s\s\s\s\s\s*\spotential\sproblems\swith\sinteger\soverflow,\sthe\s\scode\suses\n!\s\s\s\s\s\s*\sadditional\sconstants\sIQ\sand\sIR\ssuch\sthat\n\s\s\s\s\s\s\s*\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s\sIM\s=\sIA*IQ\s+\sIR\n!\s\s\s\s\s\s*\sFor\sdetails\son\show\sthis\salgorithm\sworks,\srefer\sto\sthe\sfollowing\n\s\s\s\s\s\s\s*\spapers:\s\n\s\s\s\s\s\s\s*\n\s\s\s\s\s\s\s*\s\s\s\s\s\s\s\sS.K.\sPark\s&\sK.W.\sMiller,\s"Random\snumber\sgenerators:\sgood\sones\n---\s4067,4086\s----\n\s\s\s\s\s\s\s*\sGenerate\sthe\srandom\snumber\susing\sthe\slinear\scongruential\n\s\s\s\s\s\s\s*\sgenerator\sdefined\sby\sthe\sfollowing\srecurrence:\n\s\s\s\s\s\s\s*\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s\sseed\s=\s(\sIA\s*\sseed\s)\smod\sIM\n!\s\s\s\s\s\s*\swhere\sIA\sis\s16807\sand\sIM\sis\s(2^31)\s-\s1.\s\sThe\srecurrence\smaps\n!\s\s\s\s\s\s*\sa\sseed\sin\sthe\srange\s[1,\sIM\s-\s1]\sto\sa\snew\sseed\sin\sthat\ssame\srange.\n!\s\s\s\s\s\s*\sThe\srecurrence\smaps\sIM\sto\s0,\sand\smaps\s0\sback\sto\s0,\sso\sthose\stwo\n!\s\s\s\s\s\s*\svalues\smust\snot\sbe\sallowed\sas\sinitial\svalues\sof\sseed.\n!\s\s\s\s\s\s*\n!\s\s\s\s\s\s*\sIn\sorder\sto\savoid\spotential\sproblems\swith\sinteger\soverflow,\sthe\n!\s\s\s\s\s\s*\srecurrence\sis\simplemented\sin\sterms\sof\sadditional\sconstants\n!\s\s\s\s\s\s*\sIQ\sand\sIR\ssuch\sthat\n\s\s\s\s\s\s\s*\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s\sIM\s=\sIA*IQ\s+\sIR\n!\s\s\s\s\s\s*\sNone\sof\sthe\soperations\sin\sthe\simplementation\soverflows\sa\s32-bit\n!\s\s\s\s\s\s*\ssigned\sinteger,\sand\sthe\sC\stype\slong\sis\sguaranteed\sto\sbe\sat\sleast\n!\s\s\s\s\s\s*\s32\sbits\swide.\n!\s\s\s\s\s\s*\n!\s\s\s\s\s\s*\sFor\smore\sdetails\son\show\sthis\salgorithm\sworks,\srefer\sto\sthe\sfollowing\n\s\s\s\s\s\s\s*\spapers:\s\n\s\s\s\s\s\s\s*\n\s\s\s\s\s\s\s*\s\s\s\s\s\s\s\sS.K.\sPark\s&\sK.W.\sMiller,\s"Random\snumber\sgenerators:\sgood\sones\n***************\n***\s4079,4092\s****\n\s\s#define\sRAND_IR\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s2836\n\s\s#define\sRAND_MASK\s\s\s\s\s123459876\n\s\s\n-\s\s\s\s\sif\s(iPtr->randSeed\s==\s0)\s{\n-\s\s\s\s\s\s\s/*\n-\s\s\s\s\s\s\s\s*\sDon't\sallow\sa\s0\sseed,\ssince\sit\sbreaks\sthe\sgenerator.\s\sShift\n-\s\s\s\s\s\s\s\s*\sit\sto\ssome\sother\svalue.\n-\s\s\s\s\s\s\s\s*/\n-\s\n-\s\s\s\s\s\s\siPtr->randSeed\s=\s123459876;\n-\s\s\s\s\s}\n\s\s\s\s\s\stmp\s=\siPtr->randSeed/RAND_IQ;\n\s\s\s\s\s\siPtr->randSeed\s=\sRAND_IA*(iPtr->randSeed\s-\stmp*RAND_IQ)\s-\sRAND_IR*tmp;\n\s\s\s\s\s\sif\s(iPtr->randSeed\s<\s0)\s{\n---\s4096,4101\s----\n***************\n***\s4094,4107\s****\n\s\s\s\s\s\s}\n\s\s\n\s\s\s\s\s\s/*\n!\s\s\s\s\s\s*\sOn\s64-bit\sarchitectures\swe\sneed\sto\smask\soff\sthe\supper\sbits\sto\n!\s\s\s\s\s\s*\sensure\swe\sonly\shave\sa\s32-bit\srange.\s\sThe\sconstant\shas\sthe\n!\s\s\s\s\s\s*\sbizarre\sform\sbelow\sin\sorder\sto\smake\ssure\sthat\sit\sdoesn't\n!\s\s\s\s\s\s*\sget\ssign-extended\s(the\srules\sfor\ssign\sextension\sare\svery\n!\s\s\s\s\s\s*\sconcat,\sparticularly\son\s64-bit\smachines).\n\s\s\s\s\s\s\s*/\n\s\s\n-\s\s\s\s\siPtr->randSeed\s&=\s((((unsigned\slong)\s0xfffffff)\s<<\s4)\s|\s0xf);\n\s\s\s\s\s\sdResult\s=\siPtr->randSeed\s*\s(1.0/RAND_IM);\n\s\s\n\s\s\s\s\s\s/*\n---\s4103,4112\s----\n\s\s\s\s\s\s}\n\s\s\n\s\s\s\s\s\s/*\n!\s\s\s\s\s\s*\sSince\sthe\srecurrence\skeeps\sseed\svalues\sin\sthe\srange\s[1,\sRAND_IM\s-\s1],\n!\s\s\s\s\s\s*\sdividing\sby\sRAND_IM\syields\sa\sdouble\sin\sthe\srange\s(0,\s1).\n\s\s\s\s\s\s\s*/\n\s\s\n\s\s\s\s\s\sdResult\s=\siPtr->randSeed\s*\s(1.0/RAND_IM);\n\s\s\n\s\s\s\s\s\s/*\n***************\n***\s4248,4258\s****\n\s\s\s\s\s\s}\n\s\s\s\s\s\s\n\s\s\s\s\s\s/*\n!\s\s\s\s\s\s*\sReset\sthe\sseed.\n\s\s\s\s\s\s\s*/\n\s\s\n\s\s\s\s\s\siPtr->flags\s|=\sRAND_SEED_INITIALIZED;\n\s\s\s\s\s\siPtr->randSeed\s=\si;\n\s\s\n\s\s\s\s\s\s/*\n\s\s\s\s\s\s\s*\sTo\savoid\sduplicating\sthe\srandom\snumber\sgeneration\scode\swe\ssimply\n---\s4253,4268\s----\n\s\s\s\s\s\s}\n\s\s\s\s\s\s\n\s\s\s\s\s\s/*\n!\s\s\s\s\s\s*\sReset\sthe\sseed.\s\sMake\ssure\s1\s<=\srandSeed\s<=\s2^31\s-\s2.\n!\s\s\s\s\s\s*\sSee\scomments\sin\sExprRandFunc()\sfor\smore\sdetails.\n\s\s\s\s\s\s\s*/\n\s\s\n\s\s\s\s\s\siPtr->flags\s|=\sRAND_SEED_INITIALIZED;\n\s\s\s\s\s\siPtr->randSeed\s=\si;\n+\s\s\s\s\siPtr->randSeed\s&=\s(unsigned\slong)\s0x7fffffff;\n+\s\s\s\s\sif\s((iPtr->randSeed\s==\s0)\s||\s(iPtr->randSeed\s==\s0x7fffffff))\s{\n+\s\s\s\s\s\s\siPtr->randSeed\s^=\s123459876;\n+\s\s\s\s\s}\n\s\s\n\s\s\s\s\s\s/*\n\s\s\s\s\s\s\s*\sTo\savoid\sduplicating\sthe\srandom\snumber\sgeneration\scode\swe\ssimply\n***\stests/expr-old.test.orig\s\s\s\sFri\sNov\s\s3\s01:42:33\s2000\n---\stests/expr-old.test\sFri\sNov\s\s3\s01:41:33\s2000\n***************\n***\s825,830\s****\n---\s825,836\s----\n\s\stest\sexpr-old-32.51\s{math\sfunctions\sin\sexpressions}\s{\n\s\s\s\s\s\slist\s[catch\s{expr\s{srand([lindex\s"6ty"\s0])}}\smsg]\s$msg\n\s\s}\s{1\s{argument\sto\smath\sfunction\sdidn't\shave\snumeric\svalue}}\n+\stest\sexpr-old-32.52\s{math\sfunctions\sin\sexpressions}\s{\n+\s\s\s\s\sexpr\ssrand(1<<37)\s<\s1\n+\s}\s{1}\n+\stest\sexpr-old-32.52\s{math\sfunctions\sin\sexpressions}\s{\n+\s\s\s\s\sexpr\ssrand((1<<31)\s-\s1)\s>\s0\n+\s}\s{1}\n\s\s\n\s\stest\sexpr-old-33.1\s{conversions\sand\sfancy\sargs\sto\smath\sfunctions}\s{\n\s\s\s\s\s\sexpr\shypot\s(\s3\s,\s4\s)\n\n\n\n\n
J login dgp
J mimetype text/plain
J username dgp
K 221072ffffffffffffffffffffffffffffffffff
U dgp
Z 93d1af1cf433a454063662ce77624cfb