Skip to content

Commit 52134a8

Browse files
authored
Improve sorted sample algorithm
1 parent 1dcc77c commit 52134a8

File tree

1 file changed

+8
-8
lines changed

1 file changed

+8
-8
lines changed

src/SamplingUtils.jl

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,14 @@ function infer_eltype(itr)
1313
ifelse(T2 !== Union{} && T2 <: T1, T2, T1)
1414
end
1515

16-
function sortedrandrange(rng, range, n)
17-
exp_rands = randexp(rng, n)
18-
sorted_rands = cumsum(exp_rands)
19-
a, b = range.start, range.stop
20-
range_size = b-a+1
21-
cum_step = (sorted_rands[end] + randexp(rng)) / range_size
22-
sorted_rands ./= cum_step
23-
return ceil.(Int, sorted_rands)
16+
function sortedsample(rng, range, n)
17+
s = Vector{Int}(undef, n)
18+
curmax = -log(Float64(range.stop))
19+
for i in n:-1:1
20+
curmax += randexp(rng)/i
21+
@inbounds s[i] = ceil(Int, exp(-curmax))
22+
end
23+
return s
2424
end
2525

2626
function get_sorted_indices(rng, n, N, replace)

0 commit comments

Comments
 (0)