diff options
-rw-r--r-- | ext/spl/php_spl.c | 5 | ||||
-rw-r--r-- | ext/standard/array.c | 2 | ||||
-rw-r--r-- | ext/standard/basic_functions.c | 22 | ||||
-rw-r--r-- | ext/standard/basic_functions.h | 5 | ||||
-rw-r--r-- | ext/standard/php_mt_rand.h | 4 | ||||
-rw-r--r-- | ext/standard/php_rand.h | 12 | ||||
-rw-r--r-- | ext/standard/rand.c | 87 |
7 files changed, 17 insertions, 120 deletions
diff --git a/ext/spl/php_spl.c b/ext/spl/php_spl.c index d4d1a208a8..d352893672 100644 --- a/ext/spl/php_spl.c +++ b/ext/spl/php_spl.c @@ -39,7 +39,6 @@ #include "spl_heap.h" #include "zend_exceptions.h" #include "zend_interfaces.h" -#include "ext/standard/php_rand.h" #include "ext/standard/php_mt_rand.h" #include "main/snprintf.h" @@ -747,10 +746,6 @@ PHPAPI zend_string *php_spl_object_hash(zval *obj) /* {{{*/ intptr_t hash_handle, hash_handlers; if (!SPL_G(hash_mask_init)) { - if (!BG(mt_rand_is_seeded)) { - php_mt_srand((uint32_t)GENERATE_SEED()); - } - SPL_G(hash_mask_handle) = (intptr_t)(php_mt_rand() >> 1); SPL_G(hash_mask_handlers) = (intptr_t)(php_mt_rand() >> 1); SPL_G(hash_mask_init) = 1; diff --git a/ext/standard/array.c b/ext/standard/array.c index b7339b433c..2fc3f284ca 100644 --- a/ext/standard/array.c +++ b/ext/standard/array.c @@ -5063,7 +5063,7 @@ PHP_FUNCTION(array_rand) randval = php_rand(); - if ((double) (randval / (PHP_RAND_MAX + 1.0)) < (double) num_req / (double) num_avail) { + if ((double) (randval / PHP_RAND_MAX) <= (double) num_req / (double) num_avail) { /* If we are returning a single result, just do it. */ if (Z_TYPE_P(return_value) != IS_ARRAY) { if (string_key) { diff --git a/ext/standard/basic_functions.c b/ext/standard/basic_functions.c index e712d76216..4c1321b674 100644 --- a/ext/standard/basic_functions.c +++ b/ext/standard/basic_functions.c @@ -1883,19 +1883,6 @@ ZEND_BEGIN_ARG_INFO(arginfo_quoted_printable_encode, 0) ZEND_ARG_INFO(0, str) ZEND_END_ARG_INFO() /* }}} */ -/* {{{ rand.c */ -ZEND_BEGIN_ARG_INFO_EX(arginfo_srand, 0, 0, 0) - ZEND_ARG_INFO(0, seed) -ZEND_END_ARG_INFO() - -ZEND_BEGIN_ARG_INFO_EX(arginfo_rand, 0, 0, 0) - ZEND_ARG_INFO(0, min) - ZEND_ARG_INFO(0, max) -ZEND_END_ARG_INFO() - -ZEND_BEGIN_ARG_INFO(arginfo_getrandmax, 0) -ZEND_END_ARG_INFO() -/* }}} */ /* {{{ mt_rand.c */ ZEND_BEGIN_ARG_INFO_EX(arginfo_mt_srand, 0, 0, 0) ZEND_ARG_INFO(0, seed) @@ -2865,10 +2852,10 @@ const zend_function_entry basic_functions[] = { /* {{{ */ PHP_FE(proc_nice, arginfo_proc_nice) #endif - PHP_FE(rand, arginfo_rand) - PHP_FE(srand, arginfo_srand) - PHP_FE(getrandmax, arginfo_getrandmax) - PHP_FE(mt_rand, arginfo_mt_rand) + PHP_FALIAS(rand, mt_rand, arginfo_mt_rand) + PHP_FALIAS(srand, mt_srand, arginfo_mt_srand) + PHP_FALIAS(getrandmax, mt_getrandmax, arginfo_mt_getrandmax) + PHP_FE(mt_rand, arginfo_mt_rand) PHP_FE(mt_srand, arginfo_mt_srand) PHP_FE(mt_getrandmax, arginfo_mt_getrandmax) PHP_FE(mt_rand_mode, arginfo_mt_rand_mode) @@ -3484,7 +3471,6 @@ static void php_putenv_destructor(zval *zv) /* {{{ */ static void basic_globals_ctor(php_basic_globals *basic_globals_p) /* {{{ */ { - BG(rand_is_seeded) = 0; BG(mt_rand_is_seeded) = 0; BG(mt_rand_mode) = MT_RAND_MT19937; BG(umask) = -1; diff --git a/ext/standard/basic_functions.h b/ext/standard/basic_functions.h index c3b6cedf6d..d41fe74ada 100644 --- a/ext/standard/basic_functions.h +++ b/ext/standard/basic_functions.h @@ -191,14 +191,11 @@ typedef struct _php_basic_globals { char *CurrentStatFile, *CurrentLStatFile; php_stream_statbuf ssb, lssb; - /* rand.c */ + /* mt_rand.c */ uint32_t state[MT_N+1]; /* state vector + 1 extra to not violate ANSI C */ uint32_t *next; /* next random value is computed from here */ int left; /* can *next++ this many times before reloading */ - unsigned int rand_seed; /* Seed for rand(), in ts version */ - - zend_bool rand_is_seeded; /* Whether rand() has been seeded */ zend_bool mt_rand_is_seeded; /* Whether mt_rand() has been seeded */ zend_long mt_rand_mode; diff --git a/ext/standard/php_mt_rand.h b/ext/standard/php_mt_rand.h index 9d926b5538..bf0f6c20a1 100644 --- a/ext/standard/php_mt_rand.h +++ b/ext/standard/php_mt_rand.h @@ -25,6 +25,9 @@ #ifndef PHP_MT_RAND_H #define PHP_MT_RAND_H +#include "php_lcg.h" +#include "php_rand.h" + #define PHP_MT_RAND_MAX ((zend_long) (0x7FFFFFFF)) /* (1<<31) - 1 */ #define MT_RAND_MT19937 0 @@ -32,6 +35,7 @@ PHPAPI void php_mt_srand(uint32_t seed); PHPAPI uint32_t php_mt_rand(void); +PHPAPI zend_long php_mt_rand_range(zend_long min, zend_long max); PHP_MINIT_FUNCTION(mt_rand); diff --git a/ext/standard/php_rand.h b/ext/standard/php_rand.h index b145440d71..76a368484d 100644 --- a/ext/standard/php_rand.h +++ b/ext/standard/php_rand.h @@ -26,18 +26,14 @@ #define PHP_RAND_H #include "php_lcg.h" +#include "php_mt_rand.h" /* System Rand functions */ #ifndef RAND_MAX -#define RAND_MAX (1<<15) +#define RAND_MAX PHP_MT_RAND_MAX #endif -/* In ZTS mode we rely on rand_r() so we must use RAND_MAX. */ -#if !defined(ZTS) && (defined(HAVE_LRAND48) || defined(HAVE_RANDOM)) -#define PHP_RAND_MAX 2147483647 -#else -#define PHP_RAND_MAX RAND_MAX -#endif +#define PHP_RAND_MAX PHP_MT_RAND_MAX /* * A bit of tricky math here. We want to avoid using a modulus because @@ -65,7 +61,7 @@ * -RL */ #define RAND_RANGE(__n, __min, __max, __tmax) \ - (__n) = (__min) + (zend_long) ((double) ( (double) (__max) - (__min) + 1.0) * ((__n) / ((__tmax) + 1.0))) + (__n) = php_mt_rand_range((__min), (__max)) #ifdef PHP_WIN32 #define GENERATE_SEED() (((zend_long) (time(0) * GetCurrentProcessId())) ^ ((zend_long) (1000000.0 * php_combined_lcg()))) diff --git a/ext/standard/rand.c b/ext/standard/rand.c index 284d3b41b5..2720f55661 100644 --- a/ext/standard/rand.c +++ b/ext/standard/rand.c @@ -27,27 +27,13 @@ #include "php.h" #include "php_rand.h" - -/* SYSTEM RAND FUNCTIONS */ +#include "php_mt_rand.h" /* {{{ php_srand */ PHPAPI void php_srand(zend_long seed) { -#ifdef ZTS - BG(rand_seed) = (unsigned int) seed; -#else -# if defined(HAVE_SRANDOM) - srandom((unsigned int) seed); -# elif defined(HAVE_SRAND48) - srand48(seed); -# else - srand((unsigned int) seed); -# endif -#endif - - /* Seed only once */ - BG(rand_is_seeded) = 1; + php_mt_srand(seed); } /* }}} */ @@ -55,74 +41,7 @@ PHPAPI void php_srand(zend_long seed) */ PHPAPI zend_long php_rand(void) { - zend_long ret; - - if (!BG(rand_is_seeded)) { - php_srand(GENERATE_SEED()); - } - -#ifdef ZTS - ret = php_rand_r(&BG(rand_seed)); -#else -# if defined(HAVE_RANDOM) - ret = random(); -# elif defined(HAVE_LRAND48) - ret = lrand48(); -# else - ret = rand(); -# endif -#endif - - return ret; -} -/* }}} */ - -/* {{{ proto void srand([int seed]) - Seeds random number generator */ -PHP_FUNCTION(srand) -{ - zend_long seed = 0; - - if (zend_parse_parameters(ZEND_NUM_ARGS(), "|l", &seed) == FAILURE) - return; - - if (ZEND_NUM_ARGS() == 0) - seed = GENERATE_SEED(); - - php_srand(seed); -} -/* }}} */ - -/* {{{ proto int rand([int min, int max]) - Returns a random number */ -PHP_FUNCTION(rand) -{ - zend_long min; - zend_long max; - zend_long number; - int argc = ZEND_NUM_ARGS(); - - if (argc != 0 && zend_parse_parameters(argc, "ll", &min, &max) == FAILURE) - return; - - number = php_rand(); - if (argc == 2) { - RAND_RANGE(number, min, max, PHP_RAND_MAX); - } - - RETURN_LONG(number); -} -/* }}} */ - -/* {{{ proto int getrandmax(void) - Returns the maximum value a random number can have */ -PHP_FUNCTION(getrandmax) -{ - if (zend_parse_parameters_none() == FAILURE) { - return; - } - - RETURN_LONG(PHP_RAND_MAX); + return php_mt_rand(); } /* }}} */ |