Created
June 25, 2020 16:26
-
-
Save SVMBrown/31d21bc31009c2412cbd153503888c02 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 gen-doors [] | |
(update (vec (repeat 3 0)) (rand-int 3) inc)) | |
(defn stay [doors chosen] | |
(doors chosen)) | |
(defn switch [doors chosen] | |
(let [reveal (rand-nth | |
(keep | |
(fn [[idx v]] | |
(when (and (not= idx chosen) (= 0 v)) | |
idx)) | |
(map-indexed vector doors)))] | |
(doors (some | |
#(when ((complement #{chosen reveal}) %) | |
%) | |
(range 3))))) | |
(defn simulate [trials] | |
(reduce | |
(fn [acc [doors chosen]] | |
(-> acc | |
(update :stay + (stay doors chosen)) | |
(update :switch + (switch doors chosen)))) | |
{:stay 0 | |
:switch 0} | |
(take trials (repeatedly (fn [] [(gen-doors) (rand-int 3)]))))) | |
(defn score [trials] | |
(let [{:keys [stay switch]} (simulate trials)] | |
{:stay (float (/ stay trials)) | |
:switch (float (/ switch trials))})) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment