note 23565 added to function.array-rand

From: Date: Mon, 22 Jul 2002 22:58:23 +0000
Subject: note 23565 added to function.array-rand
Groups: php.notes 
Request: Send a blank email to php-notes+get-33390@lists.php.net to get a copy of this message
If you just want to pull one random element from an array, try something like this: mt_srand((double) microtime() * 1000000); $myarray = array("this", "is", "a", "test", "to", "see", "if", "I", "can", "pull", "one", "element", "from", "an", "array", "randomly"); $random_index = mt_rand(0, (count($myarray)-1)); Then to test the randomness or what have you try a simple: $string = ""; // Just to kill the warning for ($i=0; $i<count($myarray); $i++) { $random_index = mt_rand(0, (count($myarray)-1)); $string .= "$myarray[$random_index] "; } $string = rtrim($string); echo ($string); I've gotten extremely good output from this method and would recommend it if you're just pulling one element. -- http://www.php.net/manual/en/function.array-rand.php http://master.php.net/manage/user-notes.php?action=edit+23565 http://master.php.net/manage/user-notes.php?action=delete+23565 http://master.php.net/manage/user-notes.php?action=reject+23565

« previous php.notes (#33390) next »