From 0cb6a319064cfe2743cdffe248359bf8d5133c0a Mon Sep 17 00:00:00 2001 From: Jeroen van Wolffelaar Date: Mon, 3 Sep 2001 01:54:04 +0000 Subject: [PATCH] Fix array_rand and array_suffle to use php_rand correctly --- ext/standard/array.c | 22 ++++++---------------- 1 file changed, 6 insertions(+), 16 deletions(-) diff --git a/ext/standard/array.c b/ext/standard/array.c index 444820458a..8f7d46e083 100644 --- a/ext/standard/array.c +++ b/ext/standard/array.c @@ -1377,7 +1377,7 @@ PHP_FUNCTION(range) static int array_data_shuffle(const void *a, const void*b) { - return (php_rand() % 2) ? 1 : -1; + return php_rand_range(0,1) ? 1 : -1; } @@ -2716,18 +2716,18 @@ PHP_FUNCTION(array_multisort) /* {{{ proto mixed array_rand(array input [, int num_req]) Return key/keys for random entry/entries in the array */ + +/* FIXME:The algorithm used is bogus! */ PHP_FUNCTION(array_rand) { zval **input, **num_req; - long randval; + double randval; int num_req_val, num_avail, key_type; char *string_key; uint string_key_len; ulong num_key; HashPosition pos; - php_error(E_ERROR, "Function array_rand temporarily disabled"); - if (ZEND_NUM_ARGS() < 1 || ZEND_NUM_ARGS() > 2 || zend_get_parameters_ex(ZEND_NUM_ARGS(), &input, &num_req) == FAILURE) { WRONG_PARAM_COUNT; @@ -2760,20 +2760,10 @@ PHP_FUNCTION(array_rand) zend_hash_internal_pointer_reset_ex(Z_ARRVAL_PP(input), &pos); while (num_req_val && (key_type = zend_hash_get_current_key_ex(Z_ARRVAL_PP(input), &string_key, &string_key_len, &num_key, 0, &pos)) != HASH_KEY_NON_EXISTANT) { -#ifdef HAVE_RANDOM - randval = random(); -#else -#ifdef HAVE_LRAND48 - randval = lrand48(); -#else - randval = rand(); -#endif -#endif - /* TEMPORARY HACK TO GET IT COMPILE */ -#define PHP_RAND_MAX 1 - if ((double)(randval/(PHP_RAND_MAX+1.0)) < (double)num_req_val/(double)num_avail) { + randval = php_drand(); + if (randval < (double)num_req_val/(double)num_avail) { /* If we are returning a single result, just do it. */ if (Z_TYPE_P(return_value) != IS_ARRAY) { if (key_type == HASH_KEY_IS_STRING) { -- 2.50.1