Created
March 8, 2014 03:46
-
-
Save shaxbee/9425020 to your computer and use it in GitHub Desktop.
Ordered random sample generator
This file contains hidden or 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
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