Oh, that's overcomplicated 🙂
function sample($min,$max,$count)
{
// Trivial cases
if($count>$max-$min+1) return false; // unsatisfiable
if($count<=0) return array();
if($min==$max) return array($min);
if($count==1) return array(mt_rand($min,$max));
return array_rand(array_flip(range($min,$max)), $count);
}
But this is assuming much more about the problem than was given in the question. For all it said the following might have been what was wanted:
function no_repeats($source=null)
{
static $reserve = null;
static $backup = null;
if(is_array($source))
{
$backup = $reserve = $source;
shuffle($reserve);
return;
}
if(count($reserve)==0)
{
$reserve = $backup;
shuffle($reserve);
}
return array_shift($reserve);
}
no_repeats(range(1,100));
for($i=0; $i<20; $i++)
echo no_repeats(),' ';