Last active
August 21, 2017 06:16
-
-
Save morfioce/64d46e46031488dec740fae001bab2be to your computer and use it in GitHub Desktop.
Knuth's p shuffle algorithm.
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
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