3 ms·
Ok I'll take a shot at it. Pick a number between 0 and N. Let's call it p for pivot. Each draw after this initial draw is either less than p with probability p
by IIAOPSW 7y ago
Ok I'll take a shot at it.
Pick a number between 0 and N. Let's call it p for pivot. Each draw after this initial draw is either less than p with probability p/(N-1) or greater than p with probability 1-p/(N-1). You need to make k-1 additional draws. The probability of m of those draws being less than p is given by the binomial distribution. Choose m from the binomial distribution. Then run the algorithm recursively on the space 0 to p-1 and p+1 to N. Some pseudo-code.
def deckpicker(N,k,offset=0):
if k == 0:
return
p = uniform_dist(0,N)
m = binomial_dist(k-1, p/(N-1))
deckpicker(p-1,m,offset)
print(p+offset)
deckpicker(N-(p+1),(k-1)-m,offset+p+1)