Last active
August 29, 2015 14:15
-
-
Save zakwilson/928e2e1611f14be35506 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
(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