Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Monty Hall Simulation
#1
Quote:>>  I wrote: (https://qb64phoenix.com/forum/showthread...50#pid5250):
> I agree (.. nonsense).
> So many internet bytes have been wasted on .99999.. = 1.  Reminds me of the excessive threads regarding the Monty Hall "problem".
> But it IS worth the time to get the not-too-difficult "solution" to the Monty Hall problem.  It is not immediately obvious.

Quote:>>  @Jack wrote:
> all talk and no code, why don't you show us a dignified answer?

Thanks for the challenge.

The classic 'Monty Hall' problem is interesting:  (from https://en.wikipedia.org/wiki/Monty_Hall_problem)

Suppose you're on a game show, and you're given the choice of three doors:
Behind one door is a car; behind the others, goats.
You pick a door, say #1.
The host opens another door, say door #3, which has a goat.
He then says to you, 'Do you want to change your pick to door #2?'
Is it to your advantage to switch your choice?

If you "stick" (don't switch), the probability is simple:
  P(win) = P(choice is car) = 1/3
If you "switch":
  P(win) = P(choice is not the car) = 2/3
  because the host will then open the other non-car door
  and offer you the third door, which has to have the car.

But that's just MY take.  There has been enormous discussion on the subject.  Hit the link above for a BIG read.

This program simulates many trials of the Monty Hall "problem".
It pretty much confirms the 1/3 - 2/3 probabilities.

Code: (Select All)
_Title "Monty Hall Simulator" ' dcromley
Option _Explicit
Dim s$, pause$, n
Dim Shared carDoor, choiceDoor, openDoor, offerDoor
Dim stickerWins, stickerLosses, switcherWins, switcherLosses
Randomize Timer
Locate 2, 6: Print " '1' for single trial; '2' for continuous running; ESC to exit"
pause$ = "1"
Do
  n = n + 1
  Locate 4, 2: Print "Trial#";: Print Using "#,###,###,###"; n
  ' get setup for this n
  carDoor = 1 + Int(Rnd * 3) ' the car, 1-3
  choiceDoor = 1 + Int(Rnd * 3) ' the choice,1-3
  openDoor = getopenDoor ' host opens a non-car Door
  offerDoor = getofferDoor ' the offer is not the choiceDoor, not the openDoor
  Locate , 2: Print "carDoor="; carDoor
  Locate , 2: Print "choiceDoor="; choiceDoor
  Locate , 2: Print "openDoor="; openDoor
  Locate , 2: Print "offerDoor="; offerDoor
  ' -- the sticker (non-switcher) --
  If choiceDoor = carDoor Then stickerWins = stickerWins + 1 Else stickerLosses = stickerLosses + 1
  ' -- the switcher --
  If offerDoor = carDoor Then switcherWins = switcherWins + 1 Else switcherLosses = switcherLosses + 1
  ' post results
  Locate , 2
  print using "Non-Switcher: Wins=#,###,###,### Losses=#,###,###,### Percent=###.###"; _
    stickerWins;stickerLosses;100*stickerWIns/n
  Locate , 2
  print using "Switcher:     Wins=#,###,###,### Losses=#,###,###,### Percent=###.###"; _
    switcherWins;switcherLosses;100*switcherWIns/n
  If pause$ = "1" Then
    Do
      pause$ = InKey$
    Loop While pause$ = ""
  Else
    pause$ = InKey$
  End If
Loop Until pause$ = Chr$(27)

Function getopenDoor () ' open a non-car door
  Dim r
  Do
    r = 1 + Int(Rnd * 3)
  Loop Until r <> choiceDoor And r <> carDoor
  getopenDoor = r
End Function

Function getofferDoor () ' offer the non-open door
  Dim r
  Do
    r = 1 + Int(Rnd * 3)
  Loop Until r <> choiceDoor And r <> openDoor
  getofferDoor = r
End Function
___________________________________________________________________________________
I am mostly grateful for the people who came before me.  Will the people after me be grateful for me?
Reply
#2
The odds after host opens 1 door is 50:50 if you choose to switch doors, certainly not 2/3! But the choice to switch doors is the one to pick because the odds of picking from 3 doors is 1/3 for grand prize at start, now improved to 1/2 after one door removed. I ran the sim years ago.
b = b + ...
Reply
#3
(08-16-2022, 04:23 PM)bplus Wrote: The odds after host opens 1 door is 50:50 if you choose to switch doors, certainly not 2/3! But the choice to switch doors is the one to pick because the odds of picking from 3 doors is 1/3 for grand prize at start, now improved to 1/2 after one door removed. I ran the sim years ago.

It's not.  The odds are 1/3 you picked the right door and 2/3 you picked the wrong door -- and they stay those odds for the whole game.

Something is behind door 1, 2, or 3.

You choose 1.   There's a 1 in 3 chance you guessed right.  2 in 3 you guessed wrong.

I tell you, it's not in door #3.  Do you want to swap to Door #2?

What's your odds??  Just as they were before: 1 in 3 that you guessed right.  2 in 3 you guessed wrong.  Switch to Door #2 and you have twice the chance to win as in Door #1.
Reply
#4
Yeah I just ran a sim again either the First Picker wins or the Switcher wins. If the First Picker wins 1/3 then the Switcher wins 2/3 the time! Man 2/3 the time is a shocker! I thought it only improved odds by 1/6th to 50/50 but no, I was wrong,

Here is my sim that I used to prove to myself:
Code: (Select All)
Option _Explicit
Dim As Long trials, t, i, grandPrize, pick1, doorOpen, doorSwitch, OnePicker, Switcher
trials = 100
For t = 1 To trials
    grandPrize = Int(Rnd * 3) + 1 ' door 1 to 3
    pick1 = Int(Rnd * 3) + 1
    For i = 1 To 3
        If i <> grandPrize And i <> pick1 Then doorOpen = i: Exit For
    Next
    For i = 1 To 3 ' find doorSwitch
        If i <> pick1 And i <> doorOpen Then doorSwitch = i: Exit For
    Next
    If grandPrize = pick1 Then OnePicker = OnePicker + 1
    If grandPrize = doorSwitch Then Switcher = Switcher + 1
Next
Print "One picker wins ="; OnePicker
Print "Switcher wins = "; Switcher
b = b + ...
Reply
#5
Here's my sim:

Code: (Select All)
Dim door(1 To 3)
Const Limit = 1000 'number of games to play without switching, and then repeat that number and switch
Randomize Timer


For i = 1 To Limit
    Print "Game #"; i
    prize = Int(Rnd * 3) + 1 'which door is the prize behind?
    Print "Is it behind Door #1, #2, or #3?"
    guess = Int(Rnd * 3) + 1 'where is our initial guess
    Print "I guess door #"; guess
    'here we can waste some time not swapping
    If guess = prize Then eliminate = guess + 1: If eliminate > 3 Then eliminate = 1

    Print "It's not behind door #"; eliminate; "!  Will you swap?"
    Print "Nope!  I want to keep my choice."
    Print "Since you're keeping it, let's see what happens!"
    If guess = prize Then
        win = win + 1
        Print "You win the car!"
    Else
        lose = lose + 1
        Print "Sorry!  It was behind door #"; prize
    End If
Next

Print "Of "; Limit; "games, you won "; win; "times and lost "; lose; "times by not swapping!"
Sleep


For i = 1 To Limit
    Print "Game #"; i
    prize = Int(Rnd * 3) + 1 'which door is the prize behind?
    Print "Is it behind Door #1, #2, or #3?"
    guess = Int(Rnd * 3) + 1 'where is our initial guess
    Print "I guess door #"; guess
    'here we can waste some time not swapping
    If guess = prize Then eliminate = guess + 1: If eliminate > 3 Then eliminate = 1

    Print "It's not behind door #"; eliminate; "!  Will you swap?"
    Print "Nope!  I want to keep my choice."
    Print "Since you're keeping it, let's see what happens!"
    If guess = prize Then
        lose2 = lose2 + 1
        Print "Sorry!  It was behind door #"; eliminate
    Else
        win2 = win2 + 1
        Print "You win the car!"

    End If
Next

Print "Of "; Limit; "games, you won "; win2; "times and lost "; lose2; "times by swapping!"
Print
Print "To compare:"
Print "Of "; Limit; "games, you won "; win; "times and lost "; lose; "times by not swapping!"
Reply
#6
Wait... the Switcher is only going to win 1/2 the time against the show and get the car or other Grand Prize.

1/2 is better than 1/3 but switcher won't win 2/3 the time.

Eh, no when a door is opened odds are 50:50 to get it right but first picker is stuck with their choice 1/3 chance, so Switcher takes up slack 2/3's.

So show wins 2/3 of time against a First picker and 1/3 time against a Switcher.
b = b + ...
Reply
#7
(08-16-2022, 05:09 PM)bplus Wrote: Wait... the Switcher is only going to win 1/2 the time against the show and get the car or other Grand Prize.

The switcher wins 2/3 of the time.  We just showed that with the simulation runs.  Wink
Reply
#8
Yeah I appended to that post a correction, somewhere I had in my memory the show wins 50% or 50% was a relevant consideration. Maybe the First picker wins 50% less than Switcher.
b = b + ...
Reply
#9
Here's one to think about: The state lottery makes a mistake and prints 10 scratch-off lottery tickets. One of them has a $1,000,000 prize to it, the rest are losers. Hearing the news, you quickly run down to the local store and buy they only ticket they have for $1000. Too nervous to scratch it off, you stick it in your pocket and go to work.

During your shift, the state rushes out and manages to buy up 8 of the remaining tickets. Scratching them they confirm they're all losers.

Now, what's the chance your ticket is a winner?? If your neighbor somehow bought the other ticket, and wanted to trade, would your odds be better or worse by trading?
Reply
#10
"Now, what's the chance your ticket is a winner"
easy, 50%
Reply




Users browsing this thread: 3 Guest(s)