Skip to content

Instantly share code, notes, and snippets.

@morfioce
Last active August 21, 2017 07:13
Show Gist options
  • Save morfioce/21e364fa668d51ef28ada8130d3d5636 to your computer and use it in GitHub Desktop.
Save morfioce/21e364fa668d51ef28ada8130d3d5636 to your computer and use it in GitHub Desktop.
An example of a biased shuffle
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