def sample(n, r):
    "Generate r randomly chosen, sorted integers from [0,n)"
    rand = random.random
    pop = n
    for samp in xrange(r, 0, -1):
        cumprob = 1.0
        x = rand()        
        while x < cumprob:
            cumprob -= cumprob * samp / pop
            pop -= 1
        yield n-pop-1
Generate r randomly chosen, sorted integers from [0,n)
recipes/Python/272884_Random_samples_without/recipe-272884.py