To us, here and now, it appears thus.

rants, ramblings and other things that make life worth living…

The Monty Hall problem and LISP

leave a comment »

I recently stumbled upon the Monty Hall problem at Wikipedia. I’ve seen this problem before, and have always used good old bayes theorem to solve it. Since, I was hacking at some boring regular expressions and needed to take a break, and also Wikisource site has C/C++/Java and Perl simulations but none in LISP, so I decided to try my hand at it.

Since, I am not some ‘uber-lisp-hacker’ this is not the most efficient code one can write and if some uber lisp hacker finds this , maybe he can improve it.

Anyway here is the code..

;; Monty Hall.LISP
;; Simulation for the Monty Hall problem as in Wikipedia..

(let ((host 0) (stick-wins 0) (switch-wins 0) (total-trials 3000))
&nbsp&nbsp(dotimes (i total-trials)
&nbsp&nbsp&nbsp ; chose random postions for car and goat, choices for contestant

&nbsp&nbsp&nbsp&nbsp(let* ((car (random 3))
&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp(goat1 (cond ((= car 0) 1) ((= car 1) 2) ((= car 2) 0)))
&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp(goat2 (cond ((= goat1 0) 1) ((= goat1 1) 2) ((= goat1 2) 0)))
&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp(sticker (random 3))
&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp(switcher sticker))

&nbsp&nbsp&nbsp ; select the door that the host opens and
&nbsp&nbsp&nbsp ; the door the switcher switches too..
&nbsp&nbsp&nbsp&nbsp(setf host (cond ((= sticker goat1) goat2) (t goat1)))
&nbsp&nbsp&nbsp&nbsp(setf switcher (cond ((= host goat2) car)
&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp(t (cond ((= switcher car) goat2)
&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp((= switcher goat2) car)))))
&nbsp&nbsp&nbsp&nbsp(if (= sticker car) (incf stick-wins))
&nbsp&nbsp&nbsp&nbsp(if (= switcher car) (incf switch-wins))

&nbsp&nbsp&nbsp&nbsp(format t “Car:~D, Goat 1:~D, Goat 2:~D, Sticker:~D, Switcher:~D~%”
&nbsp&nbsp&nbsp&nbsp&nbsp&nbspcar goat1 goat2 sticker switcher)))

&nbsp&nbsp (format t “Total Trials : ~D, Sticker Wins ~D times(~D %),
&nbsp&nbsp&nbsp&nbsp Switcher Wins ~D times (~D %)~%”
&nbsp&nbsp total-trials
&nbsp&nbsp stick-wins
&nbsp&nbsp (* (/ stick-wins total-trials) 100.00)
&nbsp&nbsp switch-wins
&nbsp&nbsp (* (/ switch-wins total-trials) 100.00)))

Signing Off,
Vishnu Vyas


Written by vishnuvyas

August 28, 2005 at 9:02 pm

Posted in Geeky Stuff

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: