Created
June 12, 2017 14:24
-
-
Save deque-blog/77048bae2305957fcd6db981ffaf13e8 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 enumerated-distribution-gen | |
"Create a random generator producing weighted inputs | |
- Input: a sequence of pairs [value weight-of-value] | |
- Output: a random generator that picks values from the input | |
such that P(value) = Weight(value) / Sum(all weights)" | |
[enum-dist] | |
{:pre [(pos? (count enum-dist))]} | |
(if (= 1 (count enum-dist)) | |
(constantly (ffirst enum-dist)) | |
(let [buckets (enum-dist->buckets enum-dist)] | |
(fn [] | |
(let [[v1 v2 p] (rand-nth buckets)] | |
(if (< (rand) p) v1 v2)))))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment