Adding php_rand() and php_srand(seed) as a wrapper around random, lrand48 and rand.

This commit is contained in:
James Moore 2001-02-22 00:24:19 +00:00
parent 6e31987376
commit 118c015529
4 changed files with 31 additions and 44 deletions

View file

@ -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;
}

View file

@ -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;
}

View file

@ -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 */

View file

@ -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