Skip to content

Instantly share code, notes, and snippets.

@shaxbee
Created March 8, 2014 03:46
Show Gist options
  • Save shaxbee/9425020 to your computer and use it in GitHub Desktop.
Save shaxbee/9425020 to your computer and use it in GitHub Desktop.
Ordered random sample generator
def ordered_sample(population, k):
"""
Generate k-sized ordered random sample of population
Implementation of Kramer algorithm.
"""
x = 0
size = len(population)
for i in xrange(k):
x = size - int((size - x) * (random.random() ** (1.0 / (k - i))))
yield population[x]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment