Re: Randomiser
| From: | Chris | Date: | Sun, 19 Oct 2008 21:42:15 +0000 |
| Subject: | Re: Randomiser | ||
| References: | 1 2 3 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-282108@lists.php.net to get a copy of this message | ||
Andrew Barnett wrote:
I ended up using an example from the PHP website. <?php $input = array("Neo", "Morpheus", "Trinity", "Cypher", "Tank"); $rand_keys = array_rand($input, 2);You're randomizing the whole array which when it gets large, will be noticeable. If you're using zero based arrays (like above - 'Neo' is item '0', 'Morpheus' is item '1'), I'd suggest something like this: $input = array (...); $num_keys = count($input); $random_keys = array(); $num_to_fetch = 5; for ($fetched = 0; $fetched < $num_to_fetch; $fetched++) { $key = rand(0, $num_keys); // if the key has already been picked, // decrease "fetched" and try again if (in_array($key, $random_keys)) {
$fetched--;
continue;
}
$random_keys[] = $key;
}
--
Postgresql & php tutorials
http://www.designmagick.com/