From 30f1001553a0308024d681c2197c7f4661af1b80 Mon Sep 17 00:00:00 2001 From: foobar Date: Mon, 11 Aug 2003 00:43:32 +0000 Subject: [PATCH] MFH: - Fixed bug #25007 (rand() & mt_rand() seed RNG every call). --- NEWS | 1 + ext/standard/php_rand.h | 6 ++++++ ext/standard/rand.c | 14 ++++++-------- 3 files changed, 13 insertions(+), 8 deletions(-) diff --git a/NEWS b/NEWS index ffa5426018..a810e6730b 100644 --- a/NEWS +++ b/NEWS @@ -1,6 +1,7 @@ PHP 4 NEWS ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| ?? Aug 2003, Version 4.3.3RC4 +- Fixed bug #25007 (rand() & mt_rand() seed RNG every call). (Jani) - Fixed bug #24989 (external libexpat conflicts with bundled libexpat). (Jani) - Fixed bug #24980 (array_reduce() uses first element as default running total). (Ilia) diff --git a/ext/standard/php_rand.h b/ext/standard/php_rand.h index 5a718bdd52..1d6dd39384 100644 --- a/ext/standard/php_rand.h +++ b/ext/standard/php_rand.h @@ -45,6 +45,12 @@ /* MT Rand */ #define PHP_MT_RAND_MAX ((long) (0x7FFFFFFF)) /* (1<<31) - 1 */ +#ifdef PHP_WIN32 +#define GENERATE_SEED() ((long) (time(0) * GetCurrentProcessId() * 1000000 * php_combined_lcg(TSRMLS_C))) +#else +#define GENERATE_SEED() ((long) (time(0) * getpid() * 1000000 * php_combined_lcg(TSRMLS_C))) +#endif + PHPAPI void php_srand(long seed TSRMLS_DC); PHPAPI long php_rand(TSRMLS_D); PHPAPI void php_mt_srand(php_uint32 seed TSRMLS_DC); diff --git a/ext/standard/rand.c b/ext/standard/rand.c index e91853fa74..08e6b52adc 100644 --- a/ext/standard/rand.c +++ b/ext/standard/rand.c @@ -60,6 +60,9 @@ PHPAPI void php_srand(long seed TSRMLS_DC) srand((unsigned int) seed); # endif #endif + + /* Seed only once */ + BG(rand_is_seeded) = 1; } /* }}} */ @@ -205,6 +208,9 @@ PHPAPI void php_mt_srand(php_uint32 seed TSRMLS_DC) for (BG(left) = 0, *s++ = x, j = N; --j; *s++ = (x *= 69069U) & 0xFFFFFFFFU); + + /* Seed only once */ + BG(mt_rand_is_seeded) = 1; } /* }}} */ @@ -253,12 +259,6 @@ PHPAPI php_uint32 php_mt_rand(TSRMLS_D) } /* }}} */ -#ifdef PHP_WIN32 -#define GENERATE_SEED() ((long) (time(0) * GetCurrentProcessId() * 1000000 * php_combined_lcg(TSRMLS_C))) -#else -#define GENERATE_SEED() ((long) (time(0) * getpid() * 1000000 * php_combined_lcg(TSRMLS_C))) -#endif - /* {{{ proto void srand([int seed]) Seeds random number generator */ PHP_FUNCTION(srand) @@ -272,7 +272,6 @@ PHP_FUNCTION(srand) seed = GENERATE_SEED(); php_srand(seed TSRMLS_CC); - BG(rand_is_seeded) = 1; } /* }}} */ @@ -289,7 +288,6 @@ PHP_FUNCTION(mt_srand) seed = GENERATE_SEED(); php_mt_srand(seed TSRMLS_CC); - BG(mt_rand_is_seeded) = 1; } /* }}} */ -- 2.40.0