Skip to content

Instantly share code, notes, and snippets.

@MikeInnes
Created Feb 24, 2014
Embed
What would you like to do?
function rand_first_index(n, k)
r = rand()
p = k/n
i = 1
while p < r
i += 1
p += (1-p)k/(n-(i-1))
end
return i
end
function ordered_sample_norep!(xs::AbstractArray, target::AbstractArray)
n = length(xs)
k = length(target)
i = 0
for j in 1:k
step = rand_first_index(n, k)
n -= step
i += step
target[j] = xs[i]
k -= 1
end
return target
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment