From: Pedro Gimeno <phpbugs at personal dot formauri dot es> Operating system: any PHP version: 5.2.0 PHP Bug Type: Math related Bug description: mt_srand() generates the same sequences with consecutive seeds
Description: ------------ When calling mt_srand with seed 0, the resulting sequence is the same as with seed 1; when calling it with seed 2, the sequence is the same as with seed 3, etc., generating the same sequences for even numbers as for these numbers + 1. The problem seems to come from this line: register php_uint32 x = (seed | 1U) & 0xFFFFFFFFU, *s = BG(state); The | 1U is apparently there to force the seed being odd, due to the fact that the initialization uses a pure multiplicative linear-congruential generator. Replacing the line: *s++ = (x *= 69069U) & 0xFFFFFFFFU); with e.g.: *s++ = (x *= 69069U, ++x) & 0xFFFFFFFFU); should eliminate the requirement that the seed be odd. The generator X <- (X*69069+1) mod 2**32 is the 'VAX generator', has decent short-term randomness properties and works fairly well for this purpose (Wikipedia's article about MT uses it). The pure multiplicative X <- (X*69069) mod 2**32 is not so well studied and does not work well with all seeds. The seeding requirements of MT are just that not all elements are zero, which is guaranteed in this case. However, please consider using e.g. the algorithm in init_genrand() in <http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/MT2002/CODES/mt19937ar.c> instead. See justification in <http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/MT2002/emt19937ar.html>. Reproduce code: --------------- <?php for ($i = 0; $i < 10; $i++) { mt_srand($i); echo mt_rand(0, mt_getrandmax()), ", ", mt_rand(0, mt_getrandmax()), "\n"; } ?> Expected result: ---------------- All lines different. Actual result: -------------- Lines are equal by pairs. -- Edit bug report at http://bugs.php.net/?id=40114&edit=1 -- Try a CVS snapshot (PHP 4.4): http://bugs.php.net/fix.php?id=40114&r=trysnapshot44 Try a CVS snapshot (PHP 5.2): http://bugs.php.net/fix.php?id=40114&r=trysnapshot52 Try a CVS snapshot (PHP 6.0): http://bugs.php.net/fix.php?id=40114&r=trysnapshot60 Fixed in CVS: http://bugs.php.net/fix.php?id=40114&r=fixedcvs Fixed in release: http://bugs.php.net/fix.php?id=40114&r=alreadyfixed Need backtrace: http://bugs.php.net/fix.php?id=40114&r=needtrace Need Reproduce Script: http://bugs.php.net/fix.php?id=40114&r=needscript Try newer version: http://bugs.php.net/fix.php?id=40114&r=oldversion Not developer issue: http://bugs.php.net/fix.php?id=40114&r=support Expected behavior: http://bugs.php.net/fix.php?id=40114&r=notwrong Not enough info: http://bugs.php.net/fix.php?id=40114&r=notenoughinfo Submitted twice: http://bugs.php.net/fix.php?id=40114&r=submittedtwice register_globals: http://bugs.php.net/fix.php?id=40114&r=globals PHP 3 support discontinued: http://bugs.php.net/fix.php?id=40114&r=php3 Daylight Savings: http://bugs.php.net/fix.php?id=40114&r=dst IIS Stability: http://bugs.php.net/fix.php?id=40114&r=isapi Install GNU Sed: http://bugs.php.net/fix.php?id=40114&r=gnused Floating point limitations: http://bugs.php.net/fix.php?id=40114&r=float No Zend Extensions: http://bugs.php.net/fix.php?id=40114&r=nozend MySQL Configuration Error: http://bugs.php.net/fix.php?id=40114&r=mysqlcfg