Skip to content

Instantly share code, notes, and snippets.

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 deque-blog/27098bbdf07201751fa95d94f5483ade to your computer and use it in GitHub Desktop.
Save deque-blog/27098bbdf07201751fa95d94f5483ade to your computer and use it in GitHub Desktop.
(defn linear-enumerated-distribution-gen
[enum-dist]
(let [total-weight (sum-weights enum-dist)]
(fn []
(loop [searched-weight (rand total-weight)
[[value weight] & enum-dist] (seq enum-dist)]
(if (<= weight searched-weight)
(recur (- searched-weight weight) enum-dist)
value)))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment