Monty Hall and the Python #4: Automating responses and running 10,000 times

Published Aug. 10, 2021, 8:24 p.m.

Welcome back! In the last tutorial we ended by running the Monty Hall simulation 10 times and tallying the results.  This time we will figure out how to automate our responses (to make the simulation much faster) and then scale up how many simulations we run so that our tally gives us a more statistically meaningful result.

Remember we want to answer the question:  Are you better off switching your answer or not?  The correct answer is "yes" but we really want to know why.

First let us return to our code.  We will replace the fixed number of simulations to run (10) with a variable that we can change once for all at the top of the code.

how_many_sims = 10
for i in range(how_many_sims):
    ...
    ...

Then at the end of our code we used that '10' figure so we will substitute our variable in there as well.  In addition, we may go ahead and perform a convenient calculation of what percentage of the time we got the prize.

print(f'You were right {tally} out of {how_many_sims} times.')
print(f'You were right {100*tally/how_many_sims}% of the time.')

That will be a helpful calculation. 

Now we want to get rid of the 'fluff'.  To do that we will mostly get rid of the interactive bits (the inputs) and replace them with either a fixed choice or a random choice.

Since it doesn't really matter what 'my choice' is we will just make that random:

my_choice = sample(choices, 1)[0]

This should be identical to the method for choosing the 'correct choice'.

Next we will 'comment out' the inputs for whether or not we want to switch our answer and we will simply fix our answer as 'False' to start:

    switch = False
    # switch_answer = input("Do you want to switch your answer? ")
    # if 'y' in switch_answer.lower():
        # switch = True

For a cleaner output on the terminal, we should also remove the print statements for each simulation:

    # print(f"Correct choice was {correct_choice}.")  
    # print(f"My choice was {new_answer} and I was {how_did_you_do}.")

In fact, I recommend commenting out all print statements except the one at the end.  (Do that on your own!)

Now we can save and run in the terminal.  That should run extremely fast.  Lets change the 'how_many_sims' variable to 100. Still fast.  And we were correct 33% of the time!  Just as we would expect for a 1 out of 3 choice.

Now we simply switch our 'switch' variable from 'False' to 'True'.

And now we see that we picked the correct door about 66% of the time!

If we up the number of simulations to 10000, we get the same approximate result with even more statistics.  Have you figured out why switching your answer doubles your chance of picking the prize yet? 

In the next tutorial we will really generalize this problem to a problem with as many doors as we want and where Monty Hall can reveal as many doors as can be rightly revealed.  Then, hopefully, you will really see why changing your mind helps you win the prize more often.  See you in the next tutorial!

 

 

skip_nextMonty Hall and the Python #5: Expand and Understand; Monty Hall with 100 doors!
  • Monty Hall and the Python #1: Setting up the Monty Hall Problem

  • Monty Hall and the Python #2: Testing out our simulation

  • Monty Hall and the Python #3: Collecting results from several simulations

  • Monty Hall and the Python #4: Automating responses and running 10,000 times
    (currently viewing)
  • Monty Hall and the Python #5: Expand and Understand; Monty Hall with 100 doors!

  • Monty Hall and the Python #6: Name those doors OR Generate any number of unique strings.