note 26884 deleted from function.array-rand by aidan
| From: | aidan@php.net | Date: | Tue, 18 Oct 2005 05:23:25 +0000 |
| Subject: | note 26884 deleted from function.array-rand by aidan | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-97131@lists.php.net to get a copy of this message | ||
Note Submitter: office at at universalmetropolis dot com
----
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:
<?php
// 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.