diff options
author | James Moore <jmoore@php.net> | 2001-02-22 00:24:19 +0000 |
---|---|---|
committer | James Moore <jmoore@php.net> | 2001-02-22 00:24:19 +0000 |
commit | 118c015529d69e06b6970bc9185436e60313f31e (patch) | |
tree | 5b58f083918e0f364229be2d4a24e88c42a93192 /ext | |
parent | 6e31987376e066b4fb856c2f619e78bb49222e23 (diff) | |
download | php-git-118c015529d69e06b6970bc9185436e60313f31e.tar.gz |
Adding php_rand() and php_srand(seed) as a wrapper around random, lrand48 and rand.
Diffstat (limited to 'ext')
-rw-r--r-- | ext/standard/array.c | 13 | ||||
-rw-r--r-- | ext/standard/crypt.c | 18 | ||||
-rw-r--r-- | ext/standard/php_rand.h | 22 | ||||
-rw-r--r-- | ext/standard/rand.c | 22 |
4 files changed, 31 insertions, 44 deletions
diff --git a/ext/standard/array.c b/ext/standard/array.c index 3ca5559e12..490640c15a 100644 --- a/ext/standard/array.c +++ b/ext/standard/array.c @@ -1364,18 +1364,7 @@ PHP_FUNCTION(range) static int array_data_shuffle(const void *a, const void*b) { - return ( - /* This is just a little messy. */ -#ifdef HAVE_RANDOM - random() -#else -#ifdef HAVE_LRAND48 - lrand48() -#else - rand() -#endif -#endif - % 2) ? 1 : -1; + return (php_rand() % 2) ? 1 : -1; } diff --git a/ext/standard/crypt.c b/ext/standard/crypt.c index 2293b7aed8..269d06ad2a 100644 --- a/ext/standard/crypt.c +++ b/ext/standard/crypt.c @@ -85,13 +85,9 @@ extern char *crypt(char *__key,char *__salt); #define PHP_STD_DES_CRYPT 1 #endif -#if HAVE_RANDOM -#define PHP_CRYPT_RAND random() -#elif HAVE_LRAND48 -#define PHP_CRYPT_RAND lrand48() -#else -#define PHP_CRYPT_RAND rand() -#endif + +#define PHP_CRYPT_RAND php_rand() + PHP_MINIT_FUNCTION(crypt) { @@ -105,13 +101,7 @@ PHP_MINIT_FUNCTION(crypt) REGISTER_LONG_CONSTANT("CRYPT_MD5", PHP_MD5_CRYPT, CONST_CS | CONST_PERSISTENT); REGISTER_LONG_CONSTANT("CRYPT_BLOWFISH", PHP_BLOWFISH_CRYPT, CONST_CS | CONST_PERSISTENT); -#if HAVE_SRANDOM - srandom((unsigned int) time(0) * getpid() * (php_combined_lcg() * 10000.0)); -#elif HAVE_SRAND48 - srand48((long) time(0) * (long) getpid() * (long) (php_combined_lcg() * 10000.0)); -#else - srand((unsigned int) time(0) * getpid() * (php_combined_lcg() * 10000.0)); -#endif + php_srand(time(0) * getpid() * (php_combined_lcg() * 10000.0)); return SUCCESS; } diff --git a/ext/standard/php_rand.h b/ext/standard/php_rand.h index 7ef8b6d9ab..3cc4296d94 100644 --- a/ext/standard/php_rand.h +++ b/ext/standard/php_rand.h @@ -36,4 +36,26 @@ #define PHP_RAND_MAX RAND_MAX #endif +/* Define rand Function wrapper */ +#ifdef HAVE_RANDOM +#define php_rand() random() +#else +#ifdef HAVE_LRAND48 +#define php_rand() lrand48() +#else +#define php_rand() rand() +#endif +#endif + +/* Define srand Function wrapper */ +#ifdef HAVE_SRANDOM +#define php_srand(seed) srandom((unsigned int)seed) +#else +#ifdef HAVE_SRAND48 +#define php_srand(seed) srand48((long)seed) +#else +#define php_srand(seed) srand((unsigned int)seed) +#endif +#endif + #endif /* PHP_RAND_H */ diff --git a/ext/standard/rand.c b/ext/standard/rand.c index e266104b9e..5b88093d75 100644 --- a/ext/standard/rand.c +++ b/ext/standard/rand.c @@ -199,15 +199,7 @@ PHP_FUNCTION(srand) WRONG_PARAM_COUNT; } convert_to_long_ex(arg); -#ifdef HAVE_SRANDOM - srandom((unsigned int) (*arg)->value.lval); -#else -#ifdef HAVE_SRAND48 - srand48((unsigned int) (*arg)->value.lval); -#else - srand((unsigned int) (*arg)->value.lval); -#endif -#endif + php_srand((*arg)->value.lval); } /* }}} */ @@ -253,15 +245,9 @@ PHP_FUNCTION(rand) } return_value->type = IS_LONG; -#ifdef HAVE_RANDOM - return_value->value.lval = random(); -#else -#ifdef HAVE_LRAND48 - return_value->value.lval = lrand48(); -#else - return_value->value.lval = rand(); -#endif -#endif + + return_value->value.lval = php_rand(); + /* * A bit of tricky math here. We want to avoid using a modulus because * that simply tosses the high-order bits and might skew the distribution |