Created
July 29, 2008 01:05
-
-
Save ishikawa/2992 to your computer and use it in GitHub Desktop.
Ruby: Random sort an array
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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