cvs: php4 /ext/standard array.c
| From: | Jeroen van Wolffelaar | Date: | Mon, 03 Sep 2001 01:54:04 +0000 |
| Subject: | cvs: php4 /ext/standard array.c | ||
| Groups: | php.cvs | ||
| Request: | Send a blank email to php-cvs+get-7168@lists.php.net to get a copy of this message | ||
jeroen Sun Sep 2 21:54:04 2001 EDT
Modified files:
/php4/ext/standard array.c
Log:
Fix array_rand and array_suffle to use php_rand correctly
Index: php4/ext/standard/array.c
diff -u php4/ext/standard/array.c:1.129 php4/ext/standard/array.c:1.130
--- php4/ext/standard/array.c:1.129 Sun Sep 2 21:46:03 2001
+++ php4/ext/standard/array.c Sun Sep 2 21:54:04 2001
@@ -21,7 +21,7 @@
+----------------------------------------------------------------------+
*/
-/* $Id: array.c,v 1.129 2001/09/03 01:46:03 jeroen Exp $ */
+/* $Id: array.c,v 1.130 2001/09/03 01:54:04 jeroen Exp $ */
#include "php.h"
#include "php_ini.h"
@@ -1377,7 +1377,7 @@
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 @@
/* {{{ 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 @@
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) {