Skip to content

Instantly share code, notes, and snippets.

@smee
Created May 6, 2011 15:35
Show Gist options
  • Save smee/959175 to your computer and use it in GitHub Desktop.
Save smee/959175 to your computer and use it in GitHub Desktop.
Wheel of fortune for arbitrary wheel slot sizes
(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