cvs: php4 /ext/standard basic_functions.h rand.c

From: Date: Sun, 07 Oct 2001 11:52:10 +0000
Subject: cvs: php4 /ext/standard basic_functions.h rand.c
Groups: php.cvs 
Request: Send a blank email to php-cvs+get-7708@lists.php.net to get a copy of this message
sterling Sun Oct 7 07:52:10 2001 EDT Modified files: /php4/ext/standard basic_functions.h rand.c Log: @ Have rand() and mt_rand() seed automatically if srand() or mt_srand() has @ not been called. (Sterling) Index: php4/ext/standard/basic_functions.h diff -u php4/ext/standard/basic_functions.h:1.95 php4/ext/standard/basic_functions.h:1.96 --- php4/ext/standard/basic_functions.h:1.95 Fri Sep 21 17:29:14 2001 +++ php4/ext/standard/basic_functions.h Sun Oct 7 07:52:09 2001 @@ -17,7 +17,7 @@ +----------------------------------------------------------------------+ */ -/* $Id: basic_functions.h,v 1.95 2001/09/21 21:29:14 hholzgra Exp $ */ +/* $Id: basic_functions.h,v 1.96 2001/10/07 11:52:09 sterling Exp $ */ #ifndef BASIC_FUNCTIONS_H #define BASIC_FUNCTIONS_H @@ -174,7 +174,10 @@ 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: php4/ext/standard/rand.c diff -u php4/ext/standard/rand.c:1.55 php4/ext/standard/rand.c:1.56 --- php4/ext/standard/rand.c:1.55 Tue Sep 18 01:48:49 2001 +++ php4/ext/standard/rand.c Sun Oct 7 07:52:09 2001 @@ -20,7 +20,7 @@ | Based on code from: Shawn Cokus <Cokus@math.washington.edu> | +----------------------------------------------------------------------+ */ -/* $Id: rand.c,v 1.55 2001/09/18 05:48:49 sterling Exp $ */ +/* $Id: rand.c,v 1.56 2001/10/07 11:52:09 sterling Exp $ */ #include <stdlib.h> @@ -198,7 +198,7 @@ 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 @@ seed = GENERATE_SEED(); php_srand(seed TSRMLS_CC); + BG(rand_is_seeded) = 1; } /* }}} */ @@ -284,6 +285,7 @@ seed = GENERATE_SEED(); php_mt_srand(seed TSRMLS_CC); + BG(mt_rand_is_seeded) = 1; } /* }}} */ @@ -328,8 +330,11 @@ 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); } @@ -349,6 +354,10 @@ 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...

« previous php.cvs (#7708) next »