Technical Article

Monty Hall Paradox

,

"The Monty Hall Paradox is a veridical paradox in that the result appears absurd but is demonstrably true."

 

I wrote this piece of code to demonstrate that the laws of probability change drastically by simply setting the "@vSwitch_Door_Picked" variable from 1 to 0 or 0 to 1.

 

More detailed information on this paradox can be found on Wikipedia: http://en.wikipedia.org/wiki/Monty_Hall_problem

 

The code is just for fun and makes for an interesting conversation piece at best. Give it a try and enjoy!

SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED
SET NOCOUNT ON
SET ANSI_WARNINGS OFF
SET ARITHABORT OFF
SET ARITHIGNORE ON


DECLARE @vSwitch_Door_Picked AS BIT
DECLARE @vLoop_Counter_Iterations AS INT
DECLARE @vLoop_Counter_Start AS INT
DECLARE @vTimes_Won AS INT
DECLARE @vDoor_Winner AS INT
DECLARE @vDoor_Picked AS INT
DECLARE @vDoor_Excluded AS INT
DECLARE @vDoor_Available_Picks AS TABLE (door_number INT)


SET @vSwitch_Door_Picked = 1
SET @vLoop_Counter_Iterations = 1000
SET @vLoop_Counter_Start = 1
SET @vTimes_Won = 0


INSERT INTO @vDoor_Available_Picks VALUES (1)


INSERT INTO @vDoor_Available_Picks VALUES (2)


INSERT INTO @vDoor_Available_Picks VALUES (3)


WHILE @vLoop_Counter_Start <= @vLoop_Counter_Iterations
BEGIN

SELECT TOP 1
@vDoor_Winner = TV.door_number
FROM
@vDoor_Available_Picks TV
ORDER BY
NEWID ()


SELECT TOP 1
@vDoor_Picked = TV.door_number
FROM
@vDoor_Available_Picks TV
ORDER BY
NEWID ()


IF @vSwitch_Door_Picked = 1
BEGIN

SELECT TOP 1
@vDoor_Excluded = TV.door_number
FROM
@vDoor_Available_Picks TV
WHERE
TV.door_number NOT IN (@vDoor_Winner, @vDoor_Picked)
ORDER BY
NEWID ()


SELECT TOP 1
@vDoor_Picked = TV.door_number
FROM
@vDoor_Available_Picks TV
WHERE
TV.door_number NOT IN (@vDoor_Picked, @vDoor_Excluded)
ORDER BY
NEWID ()

END


IF @vDoor_Winner = @vDoor_Picked
BEGIN

SET @vTimes_Won = @vTimes_Won+1

END


SET @vLoop_Counter_Start = @vLoop_Counter_Start+1

END


SELECT
     @vTimes_Won AS times_won
    ,CONVERT (DECIMAL (18,2), (@vTimes_Won/(@vLoop_Counter_Iterations+.0))*100) AS pct_times_won

Rate

4.91 (11)

You rated this post out of 5. Change rating

Share

Share

Rate

4.91 (11)

You rated this post out of 5. Change rating