View Single Post
Old 03-11-2004, 05:49 PM   #5 (permalink)
Stan
Registered User
 
Stan's Avatar
 
Join Date: Mar 2004
Posts: 2
Stan is on a distinguished road
alternatively you could try this:
PHP Code:
<?
$array 
= array('red','yellow','blue','green');

echo 
$arrayarray_rand($array) ];
?>
array_rand() with only the array as an argument will return 1 random key.

the cool thing about the array_rand() function is that it can return more than 1 random key if you give it a second argument. this would be comparable to an sql select statement with a random clause.

so if i wanted 2 random colors from that array, i could do this:
PHP Code:
<?
$array 
= array('red','yellow','blue','green');

$rand_array_keys array_rand($array,2);

echo 
"First Random Color: " $array$rand_array_keys[0] ] . "<br>\n
     Second Random Color: " 
$array$rand_array_keys[1] ];
?>
Might not be that relevant to your situation, but this a way you could assure to grab 2 different random elements from the array.

i forget if it was the sql random, or php random function that didn't always return very random results with a small range of options to choose from.
Stan is offline   Reply With Quote