Skip to content

Instantly share code, notes, and snippets.

@rtirrell
Created July 16, 2010 06:30
Show Gist options
  • Save rtirrell/478019 to your computer and use it in GitHub Desktop.
Save rtirrell/478019 to your computer and use it in GitHub Desktop.
(ns clojure-playground.core)
(defn play
([] (play 10))
([n]
(loop [i 0
state false
turned-on (vec (repeat n false))
turned-off 0]
(if (== turned-off (dec n))
i
(let [p (rand-int n)
i (inc i)]
(if (not= p 0)
(if (and (not state) (not (turned-on p)))
(recur i true (assoc turned-on p true) turned-off)
(recur i state turned-on turned-off))
(if state
(recur i false turned-on (inc turned-off))
(recur i state turned-on turned-off))))))))
(defn main []
(let [runs (take 1000 (repeatedly #(play 100)))]
(println (quot (reduce + runs) (count runs)))))
(main)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment