Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 316663
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T08:24:17+00:00 2026-05-12T08:24:17+00:00

Through trying to explain the Monty Hall problem to a friend during class yesterday,

  • 0

Through trying to explain the Monty Hall problem to a friend during class yesterday, we ended up coding it in Python to prove that if you always swap, you will win 2/3 times. We came up with this:

import random as r

#iterations = int(raw_input("How many iterations? >> "))
iterations = 100000

doors = ["goat", "goat", "car"]
wins = 0.0
losses = 0.0

for i in range(iterations):
    n = r.randrange(0,3)

    choice = doors[n]
    if n == 0:
        #print "You chose door 1."
        #print "Monty opens door 2. There is a goat behind this door."
        #print "You swapped to door 3."
        wins += 1
        #print "You won a " + doors[2] + "\n"
    elif n == 1:
        #print "You chose door 2."
        #print "Monty opens door 1. There is a goat behind this door."
        #print "You swapped to door 3."
        wins += 1
        #print "You won a " + doors[2] + "\n"
    elif n == 2:
        #print "You chose door 3."
        #print "Monty opens door 2. There is a goat behind this door."
        #print "You swapped to door 1."
        losses += 1
        #print "You won a " + doors[0] + "\n"
    else:
        print "You screwed up"

percentage = (wins/iterations) * 100
print "Wins: " + str(wins)
print "Losses: " + str(losses)
print "You won " + str(percentage) + "% of the time"

My friend thought this was a good way of going about it (and is a good simulation for it), but I have my doubts and concerns. Is it actually random enough?

The problem I have with it is that the all choices are kind of hard coded in.

Is this a good or bad ‘simulation’ for the Monty Hall problem? How come?

Can you come up with a better version?

  • 1 1 Answer
  • 1 View
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T08:24:17+00:00Added an answer on May 12, 2026 at 8:24 am

    Your solution is fine, but if you want a stricter simulation of the problem as posed (and somewhat higher-quality Python;-), try:

    import random
    
    iterations = 100000
    
    doors = ["goat"] * 2 + ["car"]
    change_wins = 0
    change_loses = 0
    
    for i in xrange(iterations):
        random.shuffle(doors)
        # you pick door n:
        n = random.randrange(3)
        # monty picks door k, k!=n and doors[k]!="car"
        sequence = range(3)
        random.shuffle(sequence)
        for k in sequence:
            if k == n or doors[k] == "car":
                continue
        # now if you change, you lose iff doors[n]=="car"
        if doors[n] == "car":
            change_loses += 1
        else:
            change_wins += 1
    
    print "Changing has %s wins and %s losses" % (change_wins, change_loses)
    perc = (100.0 * change_wins) / (change_wins + change_loses)
    print "IOW, by changing you win %.1f%% of the time" % perc
    

    a typical output is:

    Changing has 66721 wins and 33279 losses
    IOW, by changing you win 66.7% of the time
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am working through trying to learn to program in Python and am focused
Trying to do it through a loop that traverses through the list. In the
I'm trying to work through Project Euler and I'm hitting a barrier on problem
I am trying to iterate through a list of float widths that varies. [10.5,
I am trying to write a ruby function that iterates through an array of
I'm trying to connect to a sql db through php but it seems that
Ok here I have a switch case statement which is falling through and trying
I'm trying to read through the documentation on Berkeley DB XML , and I
I was trying to programmatically go through presidential campaign contributions to see which web
I'm trying to traverse through all the subdirectories of the current directory in Perl,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.