[Project] The Monty Hall Problem

Recently I watched this Youtube video ( The Monty Hall Problem )

“Put simply: If you pick a goat then swap you will always win the car. And you have a 2/3 probability of picking a goat.”

I was so amazed by this logic and static thinking problem.

I use PowerShell to write this The Monty Hall Problem prove program and as yo can see from the program result screenshot,

there is very high chance you get a car when you swap your answer.

just for fun and for practice 🙂

The_Monty_Hall_Problem

PS C:\Users\chako> $PSVersionTable.PSVersion

Major  Minor  Build  Revision
-----  -----  -----  --------
5      1      14393  1532

Powershell script:


##################################################################
#
#   The Monty Hall Problem 
#   Youtube : https://www.youtube.com/watch?v=mhlc7peGlGg   
#
#   Raymond Liu 
#   
#
#   2017 / 09 / 13 v1
#   
################# Settings ################################


$getCar = 100
$Goat   = 0



try {
    
	for ($q=1; $q -le 10; $q++) {
	
        for ($i=1; $i -le 10; $i++) {
	
	        # the PowerShell cmdlet generates random numbers you must always set the Maximum value 
		    # to one number beyond your desired limt to fit within the accepted range
	        $TempA      = Get-Random -Maximum 4 -Minimum 1
            $TempB      = Get-Random -Maximum 4 -Minimum 1
		
            if ($TempA  -eq $TempB) {
	            $check = "X"
				$getCar--
	        } else {
	            $check = "O"
	        }
		
		    Write-Host(" " + $check + " ") -ForegroundColor Red -NoNewline
	    }
		Write-Host(" ");
	}
	
    $Goat = 100 - $getCar

    Write-Host(" `n") 
    Write-Host("Car: " + $getCar + "%`n") -ForegroundColor Yellow
    Write-Host("Goat: " + $Goat + "%`n`n") -ForegroundColor Yellow
 
} catch {
 
    Write-Host("`n[ERROR] Something Wrong. `n") -ForegroundColor Red
    Exit 1 
 
}

🙂
🙂

Powershell script with recursive:


##################################################################
#
#   The Monty Hall Problem 
#   Youtube : https://www.youtube.com/watch?v=mhlc7peGlGg   
#
#   Raymond Liu 
#   
#
#   2017 / 09 / 14 v2 recursive
#   
################# Settings ################################


$global:getCar = 100
$global:Goat   = 0

$count  = 10

$recursivecount = 1


try {
    
	Function recursive{
	
        $count--
		
		
        If($count -ge 0){
		
		    # the PowerShell cmdlet generates random numbers you must always set the Maximum value 
		    # to one number beyond your desired limt to fit within the accepted range
	        $TempA      = Get-Random -Maximum 4 -Minimum 1
            $TempB      = Get-Random -Maximum 4 -Minimum 1
		    
			if ($TempA  -eq $TempB) {
	            $check = "X"
				$global:getCar --
				#Write-Host("Car: " + $getCar + "%`n") -ForegroundColor Yellow
	        } else {
	            $check = "O"
	        }
			
            Write-Host(" " + $check + " ") -ForegroundColor Red -NoNewline
			
			if ($count -eq 0 -and  $recursivecount -lt 10) {
			    $count = 10
				$recursivecount++
				Write-Host(" ");
			}
            recursive
        }
    }

    recursive
	$Goat = 100 - $getCar

                Write-Host(" `n") 
                Write-Host("Car: " + $getCar + "%`n") -ForegroundColor Yellow
                Write-Host("Goat: " + $Goat + "%`n`n") -ForegroundColor Yellow
 
} catch {
 
    Write-Host("`n[ERROR] Something Wrong. `n") -ForegroundColor Red
    Exit 1 
 
}