Skip to content

Instantly share code, notes, and snippets.

@zakwilson
Last active August 29, 2015 14:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zakwilson/928e2e1611f14be35506 to your computer and use it in GitHub Desktop.
Save zakwilson/928e2e1611f14be35506 to your computer and use it in GitHub Desktop.
(import [clojure.set])
(defn make-doors []
(rand-nth [[true false false]
[false true false]
[false false true]]))
(defn reveal-door [choice doors]
(let [choices (filter #(not (= choice %)) [0 1 2])]
(rand-nth (filter #(not (doors %)) choices))))
(defn change [choice revealed-door]
(first (clojure.set/difference #{0 1 2} #{choice revealed-door})))
(defn no-change [choice revealed-door]
choice)
(defn montyhall [approach doors]
(let [choice (rand-int 3)]
(doors (approach choice
(reveal-door choice doors)))))
(defn simulate [n approach]
(float (/ (count (filter identity
(repeatedly n #(montyhall approach (make-doors)))))
n)))
; user> (simulate 1000000 no-change)
; 0.33387
; user> (simulate 1000000 change)
; 0.667118
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment