]> granicus.if.org Git - php/commitdiff
@ Have rand() and mt_rand() seed automatically if srand() or mt_srand() has
authorSterling Hughes <sterling@php.net>
Sun, 7 Oct 2001 11:52:09 +0000 (11:52 +0000)
committerSterling Hughes <sterling@php.net>
Sun, 7 Oct 2001 11:52:09 +0000 (11:52 +0000)
@ not been called. (Sterling)

ext/standard/basic_functions.h
ext/standard/rand.c

index 89bfe7af4e39d42516d715f3554ec940b8f8bdb9..27d9cf472cf36f9df94c44f1c0e61862653ff109 100644 (file)
@@ -174,7 +174,10 @@ typedef struct {
        php_uint32   *next;       /* next random value is computed from here */
        int      left;        /* can *next++ this many times before reloading */
 
-       unsigned int rand_seed; /* Seed for rand() */
+       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 */
     
        /* syslog.c */
        int syslog_started;
index e7cbac399db9f749669ae5f49ac97d08ddf462cd..e55ca7a6fd799240833df322582be3dcf0fd05a4 100644 (file)
@@ -198,7 +198,7 @@ PHPAPI void php_mt_srand(php_uint32 seed TSRMLS_DC)
 
        register php_uint32 x = (seed | 1U) & 0xFFFFFFFFU, *s = BG(state);
        register int    j;
-
+       
        for (BG(left) = 0, *s++ = x, j = N; --j;
                *s++ = (x *= 69069U) & 0xFFFFFFFFU);
 }
@@ -268,6 +268,7 @@ PHP_FUNCTION(srand)
                seed = GENERATE_SEED();
 
        php_srand(seed TSRMLS_CC);
+       BG(rand_is_seeded) = 1;
 }
 /* }}} */
 
@@ -284,6 +285,7 @@ PHP_FUNCTION(mt_srand)
                seed = GENERATE_SEED();
 
        php_mt_srand(seed TSRMLS_CC);
+       BG(mt_rand_is_seeded) = 1;
 }
 /* }}} */
 
@@ -328,8 +330,11 @@ PHP_FUNCTION(rand)
        if (argc != 0 && zend_parse_parameters(argc TSRMLS_CC, "ll", &min, &max) == FAILURE)
                return;
 
-       number = php_rand(TSRMLS_C);
+       if (!BG(rand_is_seeded)) {
+               php_srand(GENERATE_SEED() TSRMLS_CC);
+       }
 
+       number = php_rand(TSRMLS_C);
        if (argc == 2) {
                RAND_RANGE(number, min, max, PHP_RAND_MAX);
        }
@@ -350,6 +355,10 @@ PHP_FUNCTION(mt_rand)
        if (argc != 0 && zend_parse_parameters(argc TSRMLS_CC, "ll", &min, &max) == FAILURE)
                return;
 
+       if (!BG(mt_rand_is_seeded)) {
+               php_mt_srand(GENERATE_SEED() TSRMLS_CC);
+       }
+
        /*
         * Melo: hmms.. randomMT() returns 32 random bits...
         * Yet, the previous php_rand only returns 31 at most.