note 26884 added to function.array-rand
| From: | office at at universalmetropolis dot com | Date: | Sat, 16 Nov 2002 01:54:21 +0000 |
| Subject: | note 26884 added to function.array-rand | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-39556@lists.php.net to get a copy of this message | ||
Once again with PHP, you don't need to get too complicated.
I've had nothing but problems with array_rand() in a baseball management game I'm working
on, needing only one entry at random from a list of given choices several times per page.
Instead of array_rand(), I came up with this:
// list of options (shortened due to manual's word wrap)
$teamcolours =
array("green","red","orange","blue","purple","grey");
// retrieve one of the options at random from the array
$teamcolours = $teamcolours[rand(0,count($teamcolours))];
// print to verify it works
print $teamcolours;
it works like a charm.
remember to seed the random generator - srand() - at the top of your script.
--
http://www.php.net/manual/en/function.array-rand.php
http://master.php.net/manage/user-notes.php?action=edit+26884
http://master.php.net/manage/user-notes.php?action=delete+26884
http://master.php.net/manage/user-notes.php?action=reject+26884