]> granicus.if.org Git - php/commitdiff
Fix array_rand and array_suffle to use php_rand correctly
authorJeroen van Wolffelaar <jeroen@php.net>
Mon, 3 Sep 2001 01:54:04 +0000 (01:54 +0000)
committerJeroen van Wolffelaar <jeroen@php.net>
Mon, 3 Sep 2001 01:54:04 +0000 (01:54 +0000)
ext/standard/array.c

index 444820458a7553fdfe84c212cb50090f77ee1877..8f7d46e0832b3c514f1afd4205f6fa303188bbe3 100644 (file)
@@ -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) {