Share via


Powershell: Solving Monty Hall dilemma

The Monty Hall problem


The Monty Hall problem is a probability puzzle named after Monty Hall, the original host of the TV show Let’s Make a Deal. It’s a famous paradox that has an interesting solution. Suppose you’re on a game show, and you’re given the choice of three doors:

  • Behind one door is a car.
  • Behind 2 others - goats. 
  • You pick a door, say No. 1, and the host, who knows what’s behind the doors, opens another door, say No. 3, which has a goat. 
  • He then says to you, “Do you want to pick door No. 2?” 

PowerShell Script


Let us randomly put a prize behind one of the doors:

for($j = 0; $j  -lt 10000; $j++){
 
    do{
    $First = Get-Random -Minimum 0 -Maximum 2
    $Second = Get-Random -Minimum 0 -Maximum 2
    $Third = Get-Random -Minimum 0 -Maximum 2
 
    $Sum = $First  + $Second + $Third
    Write-Host "Sum $Sum"
    }
    while($Sum -ne 1)
 
    $i = ($First, $Second,  $Third)
    Write-Host "i: $i"

Then our contestant makes a choice:

$Choice = Get-Random -Minimum 0 -Maximum 3
Write-Host "Choice: $Choice"

The presenter opens one of the doors. It cannot be the door that the contestant chose, or the door that holds the actual prize:

do{
  $ShownGate = Get-Random -Minimum 0 -Maximum 3
 
  $OpenGate = $true
 
      if($ShownGate -eq $Choice)
      {
          $OpenGate = $false
      }
 
      if($i[$ShownGate] -eq 1)
      {
       
          $OpenGate = $false
      }
  }
  while (!$OpenGate)
 
  Write-Host "Finally Shown Gate: $ShownGate"

Our contestant changes their choice:

#change choice
 $NewChoice = 3 - ($Choice + $ShownGate)

Now, let's sum it up. Do they win if they changed their choice? Would they have been better off if they kept their original choice?

if($i[$Choice] -eq 1){
    $NumberOfNoChangeWins++;
}
 
if($i[$NewChoice] -eq 1){
    $NumberOfNewChoiceWins++;
}

Run the script a few times and find out yourself :)

Spoiler :)

Number of no change wins: 3309
Number of new  choice wins: 6691

The Best Explanation


The best explanation I found, was a comment under this article. The comment belongs to Dinesh, kudos!

There are three possible scenarios. Assume that out of A,B&C (A has car)

1.You pick A - you lose by shifting

2.You pick B - you win by shifting

3.You pick C - you win by shifting

so you have 2 out of three chances of winning by shifting !!!

Full Script


$NumberOfNoChangeWins = 0;
$NumberOfNewChoiceWins = 0;
  
# Once is random, 10 000 times gives you probability
for($j = 0; $j  -lt 10000; $j++){
  
       # Get three gates, one of which holds the prize
    do{
    $First = Get-Random -Minimum 0 -Maximum 2
    $Second = Get-Random -Minimum 0 -Maximum 2
    $Third = Get-Random -Minimum 0 -Maximum 2
  
    $Sum = $First  + $Second + $Third
    Write-Host "Sum $Sum"
    }
    while($Sum -ne 1) # only 1 can hold the prize
  
       # These are our gates
    $i = ($First, $Second,  $Third)
    Write-Host "i: $i"
  
       # The contestant makes a choice
    $Choice = Get-Random -Minimum 0 -Maximum 3
    Write-Host "Choice: $Choice"
  
  
       # The presenter opens a gate, but it cannot be the gate that the contestant chose, or  the gate that holds the actual prize
    do{
    $ShownGate = Get-Random -Minimum 0 -Maximum 3
  
    $OpenGate = $true
  
        if($ShownGate -eq $Choice)
        {
            $OpenGate = $false
        }
  
        if($i[$ShownGate] -eq 1)
        {
         
            $OpenGate = $false
        }
    }
    while (!$OpenGate)
  
    Write-Host "Finally Shown Gate: $ShownGate"
  
  
    #The contestant changes his choice (we have gates 0,1,2 that’s why the sum of 3)
    $NewChoice = 3 - ($Choice + $ShownGate)
  
        
  
       # Number of wins if  he kept his choice
    if($i[$Choice] -eq 1){
        $NumberOfNoChangeWins++;
    }
  
       # Number of wins if  he changed his choice
    if($i[$NewChoice] -eq 1){
        $NumberOfNewChoiceWins++;
    }
}
  
Write-Host "Number of no change wins: $NumberOfNoChangeWins"
Write-Host "Number of new choice wins: $NumberOfNewChoiceWins"