Re: create array of random unique integers?
| From: | Robin Vickery | Date: | Sun, 18 Feb 2001 19:05:29 +0000 |
| Subject: | Re: create array of random unique integers? | ||
| References: | 1 2 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-40461@lists.php.net to get a copy of this message | ||
>>>>> "a" == andrew <andrew@salamander.net> writes:
> Hi, is there a quick way to create an array of integers, randomized
> and without repeats?
> e.g. if my set is 1 - 10, create an array whoese key values are 1
> through 10, with corresponding key values as a random set of the
> same 1 through 10??
> thanks in advance for any suggestions :) andrew
Yeah, this will give you an array of the numbers 1 - 10 in random
order. If you want a different range, change the parameters to suit.
<?php
function rand_array( $start, $finish ) {
srand ( (double) microtime() * 1000000);
$rand_array = range( $start, $finish );
// it really irritates me the way PHP's sort functions mess around
// with the original array rather than returning a sorted array so
// I can assign it to where I want it.
shuffle( $rand_array );
return $rand_array;
}
print_r( rand_array(1, 10) );
?>
--
Robin Vickery.................................................
BlueCarrots, 14th Floor, 20 Eastbourne Terrace, London, W2 6LE