@ Make the seed options to srand() and mt_srand() optional, if the seed is

@ not specified, the generate the most random seed possible. (Sterling)

Please, if anyone has any comments on the way I generate this seed, speak up!
This seems to be the most "random" seed I could come up with...

This commit is 100% backwards compatible :)

Add myself to the authors list cause of recent work on the file
This commit is contained in:
Sterling Hughes 2001-09-16 03:46:59 +00:00
parent d477d490b2
commit 373fc12bb1

View File

@ -15,6 +15,7 @@
| Authors: Rasmus Lerdorf <rasmus@lerdorf.on.ca> |
| Zeev Suraski <zeev@zend.com> |
| Pedro Melo <melo@ip.pt> |
| Sterling Hughes <sterling@php.net> |
| |
| Based on code from: Shawn Cokus <Cokus@math.washington.edu> |
+----------------------------------------------------------------------+
@ -112,6 +113,7 @@ PHPAPI void php_mt_srand(php_uint32 seed TSRMLS_DC)
2^31, 2^31, 2^31, ...; 2^30, 2^30, 2^30, ...; 2^29, 2^29 + 2^31,
2^29, 2^29 + 2^31, ..., etc. so I force seed to be odd below.
Even if x_initial is odd, if x_initial is 1 mod 4 then
the lowest bit of x is always 1,
@ -190,28 +192,36 @@ PHPAPI php_uint32 php_mt_rand(TSRMLS_D)
return y ^ (y >> 18);
}
/* {{{ proto void srand(int seed)
#define GENERATE_SEED() (time(0) * getpid() * 1000000 * php_combined_lcg(TSRMLS_C))
/* {{{ proto void srand([int seed])
Seeds random number generator */
PHP_FUNCTION(srand)
{
long seed;
long seed = 0;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &seed) == FAILURE)
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|l", &seed) == FAILURE)
return;
if (!seed)
seed = GENERATE_SEED();
php_srand(seed);
}
/* }}} */
/* {{{ proto void mt_srand(int seed)
/* {{{ proto void mt_srand([int seed])
Seeds Mersenne Twister random number generator */
PHP_FUNCTION(mt_srand)
{
long seed;
long seed = 0;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &seed) == FAILURE)
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|l", &seed) == FAILURE)
return;
if (!seed)
seed = GENERATE_SEED();
php_mt_srand(seed TSRMLS_CC);
}
/* }}} */