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;
}
/* {{{ 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;
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) {