Created
July 16, 2010 06:30
-
-
Save rtirrell/478019 to your computer and use it in GitHub Desktop.
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
(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