summaryrefslogtreecommitdiff
path: root/ext/standard/rand.c
diff options
context:
space:
mode:
authorSterling Hughes <sterling@php.net>2001-10-07 11:52:09 +0000
committerSterling Hughes <sterling@php.net>2001-10-07 11:52:09 +0000
commit3a50a0322fbea3ebaf795cd730e1e6cc6798d72b (patch)
tree3f2885000dc11e26d2c5b330a19bfa9d9c08399d /ext/standard/rand.c
parentbf59a8db85a6151165f16480c8fe03761226084a (diff)
downloadphp-git-3a50a0322fbea3ebaf795cd730e1e6cc6798d72b.tar.gz
@ Have rand() and mt_rand() seed automatically if srand() or mt_srand() has
@ not been called. (Sterling)
Diffstat (limited to 'ext/standard/rand.c')
-rw-r--r--ext/standard/rand.c13
1 files changed, 11 insertions, 2 deletions
diff --git a/ext/standard/rand.c b/ext/standard/rand.c
index e7cbac399d..e55ca7a6fd 100644
--- a/ext/standard/rand.c
+++ b/ext/standard/rand.c
@@ -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.