Monty Hall problem: Difference between revisions
Content added Content deleted
m (whitespace and lang tags) |
(added CL code) |
||
Line 366: | Line 366: | ||
Changing: 66.59 |
Changing: 66.59 |
||
New random choice: 49.67</pre> |
New random choice: 49.67</pre> |
||
=={{header|Common Lisp}}== |
|||
As you can see, the SBCL RNG (Mersenne Twister) is very good, given enough iterations. |
|||
<pre>(defun make-round () |
|||
(let ((array (make-array 3 |
|||
:element-type 'bit |
|||
:initial-element 0))) |
|||
(setf (bit array (random 3)) 1) |
|||
array)) |
|||
(defun show-goat (array) |
|||
(loop for i = (random 3) |
|||
when (zerop (bit array i)) |
|||
return i)) |
|||
(defun won? (array i) |
|||
(= 1 (bit array i)))</pre> |
|||
(progn (loop repeat #1=(expt 10 6) |
|||
when (won? (make-round) (random 3)) |
|||
sum 1 into result |
|||
finally (format t "Stay: ~S%~%" (float (/ result #1# 1/100)))) |
|||
(loop repeat #1# |
|||
for round = (make-round) |
|||
for initial = (random 3) |
|||
for goat = (show-goat round) |
|||
for choice = (loop for i below 3 |
|||
when (and (/= i initial) |
|||
(/= i goat)) |
|||
return i) |
|||
when (won? round choice) |
|||
sum 1 into result |
|||
finally (format t "Switch: ~S%~%" (float (/ result #1# 1/100)))))</pre> |
|||
<pre>CL-USER> (progn (loop repeat #1=(expt 10 6) |
|||
when (won? (make-round) (random 3)) |
|||
sum 1 into result |
|||
finally (format t "Stay: ~S%~%" (float (/ result #1# 1/100)))) |
|||
(loop repeat #1# |
|||
for round = (make-round) |
|||
for initial = (random 3) |
|||
for goat = (show-goat round) |
|||
for choice = (loop for i below 3 |
|||
when (and (/= i initial) |
|||
(/= i goat)) |
|||
return i) |
|||
when (won? round choice) |
|||
sum 1 into result |
|||
finally (format t "Switch: ~S%~%" (float (/ result #1# 1/100))))) |
|||
Stay: 33.2961%</pre> |
|||
Switch: 49.9627% |
|||
=={{header|C++}}== |
=={{header|C++}}== |