Re: Randomiser

From: Date: Sun, 19 Oct 2008 21:56:23 +0000
Subject: Re: Randomiser
References: 1 2 3 4  Groups: php.general 
Request: Send a blank email to php-general+get-282113@lists.php.net to get a copy of this message
Reese wrote:
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); echo $input[$rand_keys[0]] . "\n"; echo $input[$rand_keys[1]] . "\n"; ?> Just extended the array to the number of lines I needed.
That works, but shuffle() is cleaner and then you can echo things out with a foreach() expression. http://www.php.net/manual/en/function.shuffle.php
If you're doing a shuffle inside a loop, it's going to be even slower. Might not be an issue for the OP but after your array gets to a certain size, doing that will be a problem. -- Postgresql & php tutorials http://www.designmagick.com/

« previous php.general (#282113) next »