note 26884 modified in function.array-rand by vrana

From: Date: Tue, 17 Aug 2004 14:48:46 +0000
Subject: note 26884 modified in function.array-rand by vrana
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-79937@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: <?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. --was-- 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://php.net/manual/en/function.array-rand.php

« previous php.notes (#79937) next »