Array#randomly_pick( n )
class Array # If +number+ is greater than the size of the array, the method # will simply return the array itself sorted randomly def randomly_pick(number) sort_by{ rand }.slice(0...number) end end
DZone Snippets > canadaduane > array
11381 users tagging and storing useful source code snippets
Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world
Duane Johnson http://blog.inquirylabs.com/
class Array # If +number+ is greater than the size of the array, the method # will simply return the array itself sorted randomly def randomly_pick(number) sort_by{ rand }.slice(0...number) end end