Skip to content

Instantly share code, notes, and snippets.

@morfioce
Last active August 21, 2017 06:16
Show Gist options
  • Save morfioce/64d46e46031488dec740fae001bab2be to your computer and use it in GitHub Desktop.
Save morfioce/64d46e46031488dec740fae001bab2be to your computer and use it in GitHub Desktop.
Knuth's p shuffle algorithm.
import random
def swap(array, i, j):
array[i], array[j] = array[j], array[i]
def shuffle(array):
n = len(array)
for i in range(n-1):
swap(array, i, random.randrange(i,n))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment