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

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




PostPosted: Thu Dec 11, 2008 9:31 pm   Post subject: Monty-Hall problem model

This is a script I wrote to aid my learning in Ruby, with syntax-tical help from wtd. All the logic, however, was done by myself.

It is a model of the classic Monty-Hall problem. It calculates a win or loss given a command to change 'doors' or keep the same 'door'. It is run 1 million times to provide accurate results, which were exactly as I expected them to be.

Ruby:

class MontyHall
        #the parameter (boolean) allows you to choose weather or not the 'player' changes doors
        def initialize (switchDoor)
                @switchDoor = switchDoor
                @chosenDoor = nil
                @winDoor = nil
        end
       
        #this method calculates a win or loss of the game
        def calcWin                                 
                #this represents the door the 'player' chose
                @chosenDoor = rand(max = 3)             
                 #this represents the winning door
                @winDoor = rand(max = 3)               
               
               
                if @switchDoor then                              
                        if @chosenDoor == @winDoor
                                return false
                        else
                                return true
                        end
                end
                #this is evaluated if player does not switch doors
                @chosenDoor == @winDoor     
        end
        def to_s       
                "switchDoor: #{@switchDoor} chosenDoor: #{@chosenDoor} winDoor: #{@winDoor}"
        end
end

#bigger number means more acuracy
ITERATIONS = 1_000_000        

#create 1 object for true, one for false
switch = MontyHall.new(true)                   
keep = MontyHall.new(false)
#keep track of wins
numWinSwitch = 0                                                               
numWinKeep = 0


ITERATIONS.times do
        #increment if win
        numWinSwitch +=1 if switch.calcWin                
        numWinKeep +=1 if keep.calcWin
end
puts "Number of wins when new door chosen: #{numWinSwitch}"
puts "Percent wins when new door chosen: #{(numWinSwitch/(ITERATIONS/100))}"
puts "Number of wins when original door used: #{numWinKeep}"
puts "Percent wins when original door used: #{(numWinKeep/(ITERATIONS/100))}"


Comments? Things to improve? Disagree with the results?

Oh, and this is my first Ruby program that actually does something, so be really hard. (yes, be very critical, I can take it. It's how I learn).
Sponsor
Sponsor
Sponsor
sponsor
gianni




PostPosted: Thu Dec 11, 2008 11:40 pm   Post subject: Re: Monty-Hall problem model

Looks good to me, works too, haha! Only one thing really caught my eye.

You're setting the variable max to 3 twice, and you're not using it anywhere else.
Ruby:

                #this represents the door the 'player' chose
                @chosenDoor = rand(max = 3)             
                 #this represents the winning door
                @winDoor = rand(max = 3)               


If one really wanted to be pedantic I suppose one could complain about the space between the initialize method name and the param list.
Ruby:

        #the parameter (boolean) allows you to choose weather or not the 'player' changes doors
        def initialize (switchDoor)


Other than that, looks good!
octopi




PostPosted: Fri Dec 12, 2008 12:16 am   Post subject: Re: Monty-Hall problem model

Perhaps you could post some samples of the output for those who don't have/use ruby.
gianni




PostPosted: Fri Dec 12, 2008 12:18 am   Post subject: Re: Monty-Hall problem model

code:
[gianni@gianni][~/Projects/ruby/compsci.ca]
(23:29:42) $ ./monty_hall.rb
Number of wins when new door chosen: 667147
Percent wins when new door chosen: 66
Number of wins when original door used: 332934
Percent wins when original door used: 33

[gianni@gianni][~/Projects/ruby/compsci.ca]
(23:29:47) $ ./monty_hall.rb
Number of wins when new door chosen: 667341
Percent wins when new door chosen: 66
Number of wins when original door used: 333884
Percent wins when original door used: 33

[gianni@gianni][~/Projects/ruby/compsci.ca]
(23:30:20) $ ./monty_hall.rb
Number of wins when new door chosen: 666332
Percent wins when new door chosen: 66
Number of wins when original door used: 333462
Percent wins when original door used: 33

[gianni@gianni][~/Projects/ruby/compsci.ca]
(23:30:36) $
wtd




PostPosted: Fri Dec 12, 2008 12:41 am   Post subject: Re: Monty-Hall problem model

octopi @ Fri Dec 12, 2008 1:16 pm wrote:
Perhaps you could post some samples of the output for those who don't have/use ruby.


Ruby's free. Smile
Brightguy




PostPosted: Fri Dec 12, 2008 12:51 am   Post subject: RE:Monty-Hall problem model

This isn't Monty-Hall, this is 'choose a door and guess if it's right'. For Monty-Hall you need a Monty. His behaviour makes all the difference in the problem.
octopi




PostPosted: Fri Dec 12, 2008 1:16 am   Post subject: Re: Monty-Hall problem model

BrightGuy, his output would tend to disagree.

When you switch hes getting 66%
When he keeps the original door hes getting 33%
Brightguy




PostPosted: Fri Dec 12, 2008 1:37 am   Post subject: RE:Monty-Hall problem model

I don't think many people would take issue with those results. It's pretty clear the chance your first guess was wrong is 2/3.
Sponsor
Sponsor
Sponsor
sponsor
Insectoid




PostPosted: Fri Dec 12, 2008 10:16 am   Post subject: RE:Monty-Hall problem model

Yeah, the 'max' was me misreading the method description. this was dramatically changed minutes after posting.
drumstyx




PostPosted: Wed Jul 21, 2010 12:47 pm   Post subject: Re: Monty-Hall problem model

I know this thread is very old, but I was looking for info on making this in turing, and I stumbled on this. I've since finished my program in turing, but I thought I'd add to what Brightguy was saying.

The reason this isn't a Monty-Hall, but still gives the results of the Monty-Hall is because there is no door being eliminated by a computerized "Monty". A 66% win rate for switching is provided by the fact that you'll of course be wrong 33% of the time.

code:

                if @switchDoor then                               
                        if @chosenDoor == @winDoor
                                return false
                        else
                                return true
                        end
                end


When the door picked is wrong, and the door is switched, a win is reported. Since 2/3 of the switchDoor iterations will be wrong, those same 2/3 will be reported as wins. Of course the non-switch iterations are very straightforward, since if it's right it's a win, if not, it's a lose, a very clear 1/3 chance.

If there is no door being removed, it's akin to simply writing a program that just states the solution, not actually working through it.

No disrespect was meant here, just wanted to possibly help out people looking at this in future, since it confused me that there were correct results, but no Monty.

Cheers
Insectoid




PostPosted: Wed Jul 21, 2010 1:24 pm   Post subject: RE:Monty-Hall problem model

There doesn't actually have to be a door 'removed'. If the 'player' has selected the correct door, he can only possible 'switch' to an incorrect one. If he's selected an incorrect door, he can only switch to be correct. That bit of code could really be shortened to:
code:

if @switchdoor then
    return @chosendoor != @windoor
end
This could prolly be shortened even more to a single return statement (forgo the conditional altogether) but I'm lazy.
Brightguy




PostPosted: Thu Jul 22, 2010 4:37 pm   Post subject: Re: Monty-Hall problem model

But the removal of the door is the whole reason people find the problem counterintuitive in the first place. It can also create subtleties: When Monty has a choice, if he doesn't act randomly then you can do better than 2/3 in some cases.
Display posts from previous:   
   Index -> Programming, Ruby -> Ruby Submissions
View previous topic Tell A FriendPrintable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 12 Posts ]
Jump to:   


Style:  
Search: