From 0aefc33bb31e38a755bfb7ea82ae2d77ec8aa274 Mon Sep 17 00:00:00 2001 From: Adriano Meligrana <68152031+Tortar@users.noreply.github.com> Date: Sun, 27 Oct 2024 21:13:34 +0100 Subject: [PATCH] Custom implementation of weighted sampling --- src/utils/wsample.jl | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 src/utils/wsample.jl diff --git a/src/utils/wsample.jl b/src/utils/wsample.jl new file mode 100644 index 0000000..82b40e9 --- /dev/null +++ b/src/utils/wsample.jl @@ -0,0 +1,17 @@ + +""" +weighted sampling single implementation +""" +function wsample_single(a, w) + stop_w = rand() * sum(w) + partial_w = first(w) + j = 1 + for i in 2:length(w) + @inbounds partial_w += w[i] + if partial_w > stop_w + j = i - 1 + break + end + end + return a[j] +end