note 8255 modified in array_rand by zak
| From: | zak@php.net | Date: | Fri, 01 Sep 2000 23:01:06 +0000 |
| Subject: | note 8255 modified in array_rand by zak | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-3897@lists.php.net to get a copy of this message | ||
If you have not yet been able to upgrade to PHP 4, then here is a function that duplicates
array_rand for PHP3.
function fake_array_rand ($array, $num_req = 1)
{
# Ensure that $num_req has a sane value
$num_req > 0 or $num_req = 1;
$count = count ($array);
for ($n=0; $n < $num_req; ++$n)
{
# Help keep the number generator random :)
$randval
and usleep ("0.$randval");
# Seed the random number generator
# Generate a random number
srand ((double) microtime() * 10000000);
$randval = rand();
# Use the random value to 'pick' an entry from the array
# Count the number of times that the entry is picked
++$index[$randval % $count];
}
# Loop through the array
for ($n = 0; $n < $count; ++$n, next ($array))
{
# When we get to an entry that has been picked
# Put the entry into an array for output
# Don't forget to decrease the counter
while ($index[$n])
{
$out[] = key ($array);
--$index[$n];
}
}
# Return a string if only on random key was asked for
# otherwise return an array
return ($num_req == 1) ? $out[0] : $out ;
}
# This function is distributed in the hope that it will be
# useful,but WITHOUT ANY WARRANTY; without even the implied
# warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
# PURPOSE.
http://www.php.net/manual/function.array-rand.php