Skip to content

Instantly share code, notes, and snippets.

@tgross
Created February 20, 2012 01:55
Show Gist options
  • Save tgross/1867176 to your computer and use it in GitHub Desktop.
Save tgross/1867176 to your computer and use it in GitHub Desktop.
Rednsake Trivia: Pick N Random of M without Replacement
import random
def fisher_yates(n, m):
'''
Because
return random.sample(m,n)
would be boring.
'''
scratch = range(len(m))
stop = len(scratch) - n
picks = []
while len(scratch) > stop:
to_pop = random.randint(0, len(scratch) - 1)
picks.append(scratch.pop(to_pop))
return [m[p] for p in picks]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment