Created
May 6, 2011 15:35
-
-
Save smee/959175 to your computer and use it in GitHub Desktop.
Wheel of fortune for arbitrary wheel slot sizes
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 wheel-of-fortune [weights] | |
(let [sum (reduce + weights) | |
scaled (cons 0 (reductions + (map #(/ % sum) weights))) | |
intervals (indexed (partition 2 1 scaled)) | |
in-interval? (fn [[l r] val] (and (<= l val) (>= r val)))] | |
(fn [] (let [rnd-val (rand 1)] | |
(some #(when (in-interval? (second %) rnd-val) (first %)) intervals))) | |
)) | |
(take 20 (repeatedly (wheel-of-fortune [10 20 50 100]))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment