Last active
August 21, 2017 07:13
-
-
Save morfioce/21e364fa668d51ef28ada8130d3d5636 to your computer and use it in GitHub Desktop.
An example of a biased shuffle
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) | |
swapped = [False]*n | |
while not all(swapped): | |
i, j = random.randrange(n), random.randrange(n) | |
swap(array, i, j) | |
swapped[i] = swapped[j] = True |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment