Skip to content

Commit a8a553b

Browse files
committed
Add normalization to weighting scheme
1 parent 4f3079c commit a8a553b

File tree

1 file changed

+5
-1
lines changed

1 file changed

+5
-1
lines changed

src/Random.jl

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,10 @@ function Base.rand(rng::AbstractRNG, sampler::NodeSampler{N,F,W}) where {N,F,W<:
7575
end
7676
return out[]
7777
end
78-
sample_idx(rng::AbstractRNG, weights) = findfirst(cumsum(weights) .> rand(rng))::Int
78+
function sample_idx(rng::AbstractRNG, weights)
79+
normalization = sum(weights)
80+
r = rand(rng, typeof(normalization)) * normalization
81+
return findfirst(cumsum(weights) .> r)::Int
82+
end
7983

8084
end

0 commit comments

Comments
 (0)