Advertisement
Guest User

Untitled

a guest
May 10th, 2021
274
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. (defn win? [hold-the-door?]
  2.   (let [doors (set (range 3))
  3.         $$-door (rand-int 3)
  4.         contestant-init-sel (rand-int 3)
  5.         monty-sel (-> doors
  6.                       (disj contestant-init-sel $$-door)
  7.                       first)
  8.         final-sel (if hold-the-door?
  9.                     contestant-init-sel
  10.                     (-> doors
  11.                         (disj contestant-init-sel monty-sel)
  12.                         first))]
  13.     (= final-sel $$-door)))
  14.  
  15. (defn win-stats [hold-the-door? n]
  16.   (let [freqs (frequencies
  17.                (for [_ (range n)]
  18.                  (win? hold-the-door?)))
  19.         wins (get freqs true 0)
  20.         losses (get freqs false 0)]
  21.     {:win% (-> (/ wins (+ wins losses))
  22.                (* 100)
  23.                float)
  24.      :wins wins
  25.      :losses losses}))
  26.  
  27. (def holdin-dat-door (partial win-stats true))
  28. (def i-prefer-another-door (partial win-stats false))
  29.  
  30. (comment
  31.   (time (i-prefer-another-door 1e6))
  32.   "Elapsed time: 1843.44821 msecs"
  33.   {:win% 66.6149, :wins 666149, :losses 333851}
  34.  
  35.   (time (holdin-dat-door 1e6))
  36.   "Elapsed time: 1415.997953 msecs"
  37.   {:win% 33.4201, :wins 334201, :losses 665799})
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement