Skip to content

Instantly share code, notes, and snippets.

@baya
Created September 9, 2013 05:28
Show Gist options
  • Save baya/6491811 to your computer and use it in GitHub Desktop.
Save baya/6491811 to your computer and use it in GitHub Desktop.
rand find from a collection
def rand_find(num=1, &p)
scope = p.call
items = []
rand_numbers = (0...scope.count).to_a
num.times do
break if rand_numbers.size == 0
rand_number = rand_numbers.delete_at(rand rand_numbers.size)
items << scope[rand_number]
end
items
end
rand_find(8) do
User.where(status: 0)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment