Skip to content

Instantly share code, notes, and snippets.

@mindplace
Created February 17, 2017 22:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mindplace/3f3a08299651ebf4ab91de3d83254fbc to your computer and use it in GitHub Desktop.
Save mindplace/3f3a08299651ebf4ab91de3d83254fbc to your computer and use it in GitHub Desktop.
Fisher-Yates shuffle algorithm
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