@ Have rand() and mt_rand() seed automatically if srand() or mt_srand() has

@ not been called. (Sterling)
This commit is contained in:
Sterling Hughes 2001-10-07 11:52:09 +00:00
parent bf59a8db85
commit 3a50a0322f
2 changed files with 15 additions and 3 deletions

View 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;

View 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.