Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Simulating the Monty Hall problem
Index -> Programming, Turing -> Turing Submissions
View previous topic Printable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
CaptainShrimps




PostPosted: Sun Apr 28, 2013 9:43 am   Post subject: Simulating the Monty Hall problem

I wrote a program to simulate the Monty Hall problem to show my dad that the odds are only 50/50 if you randomly choose to stay with your original choice or switch to the other door. He said my code was wrong, so I'm posting it here to see what everyone thinks.

code:

%This program is supposed to simulate the Monty Hall problem.

var winID : int
winID := Window.Open ("position:top;right,graphics:1024;512")

var correct:int         %Counts the number of correct choices
var a,b,c :boolean:=false       %3 doors
var d:int       %Represents the door the prize is behind
var q:int       %1 is stay, 2 is switch, 3 is random
var ch:string(1)
var randm :boolean

proc playerchoose
    cls
    randm:=false
    put "Type 1 to always stay, type 2 to always switch, 3 for random."
    put "There are 10000 trials. The number of times the correct door was chosen is displayed."
    get q
    if q=3 then
        randm:=true
    end if
    put "Press any key to start."
    getch (ch)
end playerchoose

proc prizegen           %Generates which door the prize is behind
    a:=false
    b:=false
    c:=false
    d:=Rand.Int(1,3)
    if d=1 then
        a:= true
    elsif d=2 then
        b:=true
    elsif d=3 then
        c:=true
    end if
end prizegen

proc pickdoor                   %Randomy pick a door, then stay or switch based on input from the start
    var d:int:=Rand.Int(1,3)
    if randm=true then
        q:= Rand.Int(1,2)
    end if
    if d=1 then
        if a=true then
            if q=1 then
                correct+=1
            elsif q=2 then
                correct+=0
            end if
        elsif b=true then
            if q=1 then
                correct+=0
            elsif q=2 then
                correct+=1
            end if
        elsif c=true then
            if q=1 then
                correct+=0
            elsif q=2 then
                correct+=1
            end if
        end if
       
    elsif d=2 then
        if a=true then
            if q=1 then
                correct+=0
            elsif q=2 then
                correct+=1
            end if
        elsif b=true then
            if q=1 then
                correct+=1
            elsif q=2 then
                correct+=0
            end if
        elsif c=true then
            if q=1 then
                correct+=0
            elsif q=2 then
                correct+=1
            end if
        end if
       
    elsif d=3 then
        if a=true then
            if q=1 then
                correct+=0
            elsif q=2 then
                correct+=1
            end if
        elsif b=true then
            if q=1 then
                correct+=0
            elsif q=2 then
                correct+=1
            end if
        elsif c=true then
            if q=1 then
                correct+=1
            elsif q=2 then
                correct+=0
            end if
        end if
    end if
end pickdoor
   
proc try            %Repeat 10000 times
    for i:1..10000
        prizegen
        pickdoor
    end for
        put correct," out of 10000 correct."
end try

loop
    correct:=0
    playerchoose
    try
    put "Press any key to restart or press ESC to quit."
    getch (ch)
    if ch=(KEY_ESC) then
        Window.Close (winID)
        exit
    end if
end loop
Sponsor
Sponsor
Sponsor
sponsor
evildaddy911




PostPosted: Sun Apr 28, 2013 11:11 am   Post subject: RE:Simulating the Monty Hall problem

the output IS accurate.

* when you switch the chance of being right is 2 in 3
* when you stay, the chance of being right drops to 1 in 3
* if you randomly choose stay or switch, the chance should be the the average of the 2 options, making the chance 1.5 in 3, or 1 in 2
nullptr




PostPosted: Wed May 01, 2013 10:42 pm   Post subject: Re: Simulating the Monty Hall problem

The Monty Hall problem is one of those problems that seems so much a common-sense question that people will very stubbornly defend their answers. Good luck convincing your dad -- your code looks good to me.
DemonWasp




PostPosted: Thu May 02, 2013 12:13 am   Post subject: RE:Simulating the Monty Hall problem

In general, shorter code is easier to prove correct (or at least assure yourself it is correct). Here's my take on the code, which displays each individual trial. Even with trials = 20, the pattern is very likely to be obvious:

Turing:

const TRIALS : int := 20

var correct_stay, correct_switch : int := 0

put "DOOR    CHOICE   WINNING STRATEGY"
for i : 1 .. TRIALS
    var door : int := Rand.Int(1,3)
    var choice : int := Rand.Int(1,3)
   
    if door = choice then
        put "  ", door, "        ", choice, "     stay"
        correct_stay += 1
    else
        put "  ", door, "        ", choice, "     switch"
        correct_switch += 1
    end if 
end for

put "STAY:   ", 100 * correct_stay / TRIALS, "%"
put "SWITCH: ", 100 * correct_switch / TRIALS, "%"


The other argument you could use is this:
1. You have a 1-in-3 chance of choosing correctly at first.
2. If you chose correctly (1 in 3), then staying wins 100% of the time but switching loses 100% of the time.
3. If you chose incorrectly (2 in 3), then switching wins 100% of the time but staying loses 100% of the time.

Because case #2 has only a 1-in-3 chance of occurring, staying only wins in 1-in-3 cases.
Display posts from previous:   
   Index -> Programming, Turing -> Turing Submissions
View previous topic Tell A FriendPrintable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 4 Posts ]
Jump to:   


Style:  
Search: