Skip to content

Instantly share code, notes, and snippets.

@guitsaru
Created February 28, 2010 02:11
Show Gist options
  • Save guitsaru/317122 to your computer and use it in GitHub Desktop.
Save guitsaru/317122 to your computer and use it in GitHub Desktop.
class Array
def pick(number)
result = []
original = self.clone
if number < original.size
number.times { result << original.delete_at(Kernel.rand(original.size)) }
else
result = original
end
result
end
end
[1, 2, 3, 4].pick(2)
=> [1, 2]
[1, 2, 3, 4].pick(2)
=> [4, 3]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment