Created
February 21, 2013 02:32
-
-
Save nimamehanian/5001520 to your computer and use it in GitHub Desktop.
Shuffle an array's elements, without using Ruby's built-in methods.
This file contains 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
class Array | |
def mixup | |
length = self.length | |
while length > 0 | |
length -= 1 | |
break if length == 0 | |
randomIndex = (rand * (length - 1)).floor | |
self[length], self[randomIndex] = self[randomIndex], self[length] | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment