Skip to content

Instantly share code, notes, and snippets.

@SVMBrown
Created June 25, 2020 16:26
Show Gist options
  • Save SVMBrown/31d21bc31009c2412cbd153503888c02 to your computer and use it in GitHub Desktop.
Save SVMBrown/31d21bc31009c2412cbd153503888c02 to your computer and use it in GitHub Desktop.
(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