Skip to content

Instantly share code, notes, and snippets.

@ishikawa
Created July 29, 2008 01:05
Show Gist options
  • Save ishikawa/2992 to your computer and use it in GitHub Desktop.
Save ishikawa/2992 to your computer and use it in GitHub Desktop.
Ruby: Random sort an array
# Random sort an array (via http://d.hatena.ne.jp/tanakaBox/20070512/1178915094)
# Not perfect, but simple.
# Usage:
# >> (1..10).to_a.shuffle
# => [1, 3, 2, 5, 9, 7, 6, 8, 4, 10]
class Array
def shuffle() self.to_a.shuffle!; end
def shuffle!() self.sort! { rand(3) - 1 }; end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment