Skip to content

Instantly share code, notes, and snippets.

@bkeepers
Created February 25, 2012 16:13
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bkeepers/1909309 to your computer and use it in GitHub Desktop.
Save bkeepers/1909309 to your computer and use it in GitHub Desktop.
# puts (1..100).to_a.shuffle.inspect
class Array
def shuffle
size.downto(1) { |n| swap(n - 1, rand(n)) }
self
end
def swap(x, y)
self[x], self[y] = self[y], self[x]
end
end
@cbeust
Copy link

cbeust commented Feb 26, 2012

FYI, this doesn't create an even distribution. Check out the blog post to find out why.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment