Created
September 29, 2012 21:41
-
-
Save pascalc/3805249 to your computer and use it in GitHub Desktop.
Monty Hall
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
(defn delete [items l] | |
(remove (set items) l)) | |
(defn monty-hall | |
[switch?] | |
(let [doors (range 3) | |
car (rand-nth doors) | |
guess-1 (rand-nth doors) | |
hint (rand-nth | |
(delete [guess-1 car] doors)) | |
guess-2 (if switch? | |
(rand-nth (delete [hint guess-1] doors)) | |
guess-1)] | |
(if (= guess-2 car) | |
:win | |
:lose))) | |
(defn win-ratio [reps switch?] | |
(let [trials (repeatedly reps #(monty-hall switch?)) | |
wins (count (filter #{:win} trials))] | |
(float (/ wins reps)))) | |
;; (win-ratio 1000 true) => 0.671 | |
;; (win-ratio 1000 false) => 0.334 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment