title: Idiomatic Clojure for picking between random weighted choices
body: <p>While dabbling in Clojure I completed a small example program to pick a random choice from a list of choices.</p>
  
  <p>The basic idea is to iterate over the choices (which are assigned a weight) and turn their weights into a range, then pick a random number within the total range to select one. It might not be the most elegant design, but let's take it for granted.</p>
  
  <p>What would wo do differently compared to my example below?</p>
  
  <p>I'm not interested in overall program structure suggestions, name-spacing, etc, mostly in your approach to each function. </p>
  
  <p>I'm especially interested in how a seasoned Clojurer would tackle the "augment" function, in which I had to use the external "cur" variable to refer to the previous end of the range.</p>
  
  <pre><code>  (def colors
        (hash-map 
              :white 1,
              :red 10,
              :blue 20,
              :green 1,
              :yellow 1
         )
       )
  
      (def color-list (vec colors))
  
      (def cur 0)
  
      (defn augment [x] 
        (def name (nth x 0))
        (def val (nth x 1))
        (def newval (+ cur val))
        (def left cur)
        (def right newval)
        (def cur (+ cur val))
        [name left right]
      )
  
      (def color-list-augmented (map augment color-list))
  
      (defn within-bounds [bound]
        (def min-bound (nth bound 1))
        (def max-bound (nth bound 2))
        (and (&gt; choice min-bound) (&lt; choice max-bound))
      )
  
      (def choice (rand-nth (range cur)))
  
      (def answer 
        (first (filter within-bounds color-list-augmented))
      )
  
      (println "Random choice:" (nth answer 0))
  </code></pre>