Skip to content

Instantly share code, notes, and snippets.

@amalloy
Created January 31, 2011 21:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save amalloy/ce05ee104dd6b06e7f03 to your computer and use it in GitHub Desktop.
Save amalloy/ce05ee104dd6b06e7f03 to your computer and use it in GitHub Desktop.
static function getRandomIndices($upto, $howmany) {
$max = $upto - 1;
$candidates = range(0, $max);
if ($howmany >= $upto) {
return $candidates;
}
$indices = array();
for ($i = 0; $i < $howmany; $i++) {
$idx = rand($i, $max);
$indices[] = $candidates[$idx];
$candidates[$idx] = $candidates[$i];
}
return $indices;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment