Monty Hall problem: Difference between revisions
Content deleted Content added
Line 359: | Line 359: | ||
Changing: 66.59 |
Changing: 66.59 |
||
New random choice: 49.67</pre> |
New random choice: 49.67</pre> |
||
=={{header|Clojure}}== |
|||
<lang clojure>(ns monty-hall-problem |
|||
(:use [clojure.contrib.seq :only (shuffle)])) |
|||
(defn play-game [staying] |
|||
(let [doors (shuffle [:goat :goat :car]) |
|||
choice (rand-int 3) |
|||
[a b] (filter #(not= choice %) (range 3)) |
|||
alternative (if (= :goat (nth doors a)) b a)] |
|||
(= :car (nth doors (if staying choice alternative))))) |
|||
(defn simulate [staying times] |
|||
(let [wins (reduce (fn [counter _] (if (play-game staying) (inc counter) counter)) |
|||
0 |
|||
(range times))] |
|||
(str "wins " wins " times out of " times))) |
|||
</lang> |
|||
<lang clojure>monty-hall-problem> (println "staying:" (simulate true 1000)) |
|||
staying: wins 337 times out of 1000 |
|||
nil |
|||
monty-hall-problem> (println "switching:" (simulate false 1000)) |
|||
switching: wins 638 times out of 1000 |
|||
nil |
|||
</lang> |
|||
=={{header|Common Lisp}}== |
=={{header|Common Lisp}}== |