cvs: php4 /ext/standard rand.c

From: Date: Sun, 16 Sep 2001 03:46:59 +0000
Subject: cvs: php4 /ext/standard rand.c
Groups: php.cvs 
Request: Send a blank email to php-cvs+get-7425@lists.php.net to get a copy of this message
sterling Sat Sep 15 23:46:59 2001 EDT Modified files: /php4/ext/standard rand.c Log: @ 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 Index: php4/ext/standard/rand.c diff -u php4/ext/standard/rand.c:1.50 php4/ext/standard/rand.c:1.51 --- php4/ext/standard/rand.c:1.50 Sun Sep 9 09:29:18 2001 +++ php4/ext/standard/rand.c Sat Sep 15 23:46:59 2001 @@ -15,11 +15,12 @@ | 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> | +----------------------------------------------------------------------+ */ -/* $Id: rand.c,v 1.50 2001/09/09 13:29:18 derick Exp $ */ +/* $Id: rand.c,v 1.51 2001/09/16 03:46:59 sterling Exp $ */ #include <stdlib.h> @@ -112,6 +113,7 @@ 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,27 +192,35 @@ 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); }

« previous php.cvs (#7425) next »