Skip to content

Instantly share code, notes, and snippets.

@mjackson
Created May 20, 2011 21:19
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 mjackson/983829 to your computer and use it in GitHub Desktop.
Save mjackson/983829 to your computer and use it in GitHub Desktop.
Pick n random items from an Array quickly
class Array
# Pick +n+ random items from this array.
def pick_random(n=1)
sort_by { rand }.slice(0...n)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment