Created
February 17, 2017 22:53
-
-
Save mindplace/3f3a08299651ebf4ab91de3d83254fbc to your computer and use it in GitHub Desktop.
Fisher-Yates shuffle algorithm
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
def shuffle(array) | |
counter = array.length - 1 | |
while counter > 0 | |
# item selected from the unshuffled part of array | |
random_index = rand(counter) | |
# swap the items at those locations | |
array[counter], array[random_index] = array[random_index], array[counter] | |
# de-increment counter | |
counter -= 1 | |
end | |
array | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment