Skip to content

Instantly share code, notes, and snippets.

@pascalc
Created September 29, 2012 21:41
Show Gist options
  • Save pascalc/3805249 to your computer and use it in GitHub Desktop.
Save pascalc/3805249 to your computer and use it in GitHub Desktop.
Monty Hall
(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