Created
June 12, 2017 12:13
-
-
Save deque-blog/27098bbdf07201751fa95d94f5483ade to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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