Skip to content

Instantly share code, notes, and snippets.

@MikeInnes
Created February 24, 2014 19:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MikeInnes/9195081 to your computer and use it in GitHub Desktop.
Save MikeInnes/9195081 to your computer and use it in GitHub Desktop.
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