Skip to content

Instantly share code, notes, and snippets.

@dbasch
Created January 10, 2014 01:25
Show Gist options
  • Save dbasch/8345410 to your computer and use it in GitHub Desktop.
Save dbasch/8345410 to your computer and use it in GitHub Desktop.
(defn wrand
"given a vector of slice sizes, returns the index of a slice given a
random spin of a roulette wheel with compartments proportional to
slices."
[slices]
(let [total (reduce + slices)
r (rand total)]
(loop [i 0 sum 0]
(if (< r (+ (slices i) sum))
i
(recur (inc i) (+ (slices i) sum))))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment