]> granicus.if.org Git - php/commitdiff
Alias rand to mt_rand
authorLeigh <leigh@php.net>
Tue, 5 Jul 2016 14:09:49 +0000 (15:09 +0100)
committerLeigh <leigh@php.net>
Tue, 5 Jul 2016 14:09:49 +0000 (15:09 +0100)
ext/spl/php_spl.c
ext/standard/array.c
ext/standard/basic_functions.c
ext/standard/basic_functions.h
ext/standard/php_mt_rand.h
ext/standard/php_rand.h
ext/standard/rand.c

index d4d1a208a84e8298156ec59297f68492c1e3262d..d352893672b9f6eef601cb168c7fcc0b315650cb 100644 (file)
@@ -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;
index b7339b433c2216606966fbbca46371b3731a16fc..2fc3f284ca4a1ead2a710570138d1f309f4b99e1 100644 (file)
@@ -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) {
index e712d762161d15cefc46e957e5bdce609f79a467..4c1321b674439e14422d4d56c2d3bf6220296024 100644 (file)
@@ -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;
index c3b6cedf6db228c8f4f84a061618cdc7f9b78acf..d41fe74ada779cbf0dd65c3179d33a6dcba6973a 100644 (file)
@@ -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;
 
index 9d926b5538b29b3b51177809122e122d06e83379..bf0f6c20a12b0bfdc7f190d4e4eea08b1cc571e2 100644 (file)
@@ -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);
 
index b145440d71ff22e2d5445348dcbc7c7e45b56517..76a368484d2b1715aa481d233f206ce1eaff7eab 100644 (file)
 #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())))
index 284d3b41b5c80026457f8d2f333fad1dacbe2686..2720f55661f7c1595aa80e4cf67714e056c187c1 100644 (file)
 
 #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();
 }
 /* }}} */