Monty Hall Paradox

The basic premise is that there are three doors, behind one is a prize. The contestant makes a guess as to which door has the prize. In this case the odds of winning are one in three. But after the guess is make the host of the game opens one of the two remaining doors, showing no prize behind it. The question is should the contestant change his guess from the original door to the remaining closed door.

The simplest was to understand this is to consider that two times out of three the prize will be behind the doors that the contestant did not choose. The host will always open a door that does not contain a prize. So two thirds of the time the remaining door will have the prize.

So, one time in three the spectator will choose the winning door, but two times in three they will win by switching. Below is a simple Html/Javascript program showing this.

Html

<!DOCTYPE html>
<html>
    <head>
        <title> Monty Hall Paradox</title>
        <script
	 src="https://code.jquery.com/jquery-3.4.1.slim.min.js"
	 integrity="sha256-pasqAKBDmFT4eHoN2ndd6lN370kFiGUFyTiUHWhU7k8="
	 crossorigin="anonymous"></script>
	<script src="//cdnjs.cloudflare.com/ajax/libs/numeral.js/2.0.6/numeral.min.js"></script>	  
        <script type="text/javascript" src="./scripts/hall.js"></script>
        <style type="text/css">
           body { padding:20px 100px;}
        </style>
    </head>
    <body>
     <div id="noswitch"></div>
     <div id="switch"></div>
    </body>
</html>

Javascript

/*
    Monty Hall paradox 
*/
$(function() 
{
        
    var cycles = 10000;
    var wins = 0;
    var losses = 0;
    
    var doors = [0,0,0];
    
    for (var i = 0; i<cycles; i++)
    {
        // initialize the doors
        doors = [0,0,0];
        // randomly assign a winning doors
        doors[Math.floor(Math.random() * 3)] = 1;
        // make a guess
        doors[Math.floor(Math.random() * 3)] == 1 ? wins += 1 : losses += 1;
    }
    var perwin = numeral((wins/cycles)).format("00.0%")
    var perlose = numeral((losses/cycles)).format("00.0%")
    $("#noswitch").html("<p>Play without switching doors:</br>Wins: "+wins + " "+perwin+"<br />Losses: "+losses + " "+perlose+ "<br /></p>");
    
    wins = 0;
    losses = 0;
    var guess = 0;
    var flag = false;
    for (var i = 0; i<cycles; i++)
    {
        // initialize doors
        doors = [0,0,0]; 
        // randomly assign a winning doors
        doors[Math.floor(Math.random() * 3)] = 1;
        // make a guess
        guess = doors[Math.floor(Math.random() * 3)];
        // look at the other doors, if both are 0 then loss, else win
        flag = false;
        for (var j=0; j< 3; j++)
        {
            if (j != guess && doors[j] == 1)  // this door is one of the others and has the prize
            {
                flag = true;
            }
        }
        flag == true ? wins += 1 : losses += 1;
    }
    perwin = numeral((wins/cycles)).format("00.0%")
    perlose = numeral((losses/cycles)).format("00.0%")
    $("#switch").html("<p>Play with switching doors:</br>Wins: "+wins + " "+perwin+"<br />Losses: "+losses + " "+perlose+ "<br /></p>");
});

Output of the above program

Play without switching doors:
Wins: 3335 33.4%
Losses: 6665 66.6%

Play with switching doors:
Wins: 6604 66.0%
Losses: 3396 34.0%

References

Leave a Reply

Your email address will not be published. Required fields are marked *