1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
|
<?php
//returns an array of random keys between 2 numbers
function &UniqueRands($min, $max, $keys, $reset=true){
static $returnme = array();
if($reset) $returnme = array();
//while is used to avoid recursive the whole function
while(in_array($x = rand($min,$max),$returnme));
$returnme[] = $x;
//$keys are the number of random integers in the array
//must not be more than the sub of max - min
if($keys > count($returnme) && count($returnme) < ($max-$min))
UniqueRands($min, $max, $keys,false);
return $returnme;
}
//usage
$rands = & UniqueRands(0, 100, 30);
print_r($rands);
//with an array
$vararry = array('red', 'blue', 'green', 'yellow','black');
$rands = & UniqueRands(0, count($vararry)-1, 3);
foreach($rands as $x)
echo "$vararry[$x],";
?> |
Partager