Monty Hall Simulation
#1
Quote:>>  I wrote: (https://staging.qb64phoenix.com/showthre...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


Messages In This Thread
Monty Hall Simulation - by dcromley - 08-16-2022, 03:51 PM
RE: Monty Hall Simulation - by bplus - 08-16-2022, 04:23 PM
RE: Monty Hall Simulation - by SMcNeill - 08-16-2022, 04:46 PM
RE: Monty Hall Simulation - by bplus - 08-16-2022, 05:00 PM
RE: Monty Hall Simulation - by SMcNeill - 08-16-2022, 05:01 PM
RE: Monty Hall Simulation - by bplus - 08-16-2022, 05:09 PM
RE: Monty Hall Simulation - by SMcNeill - 08-16-2022, 05:11 PM
RE: Monty Hall Simulation - by bplus - 08-16-2022, 05:27 PM
RE: Monty Hall Simulation - by SMcNeill - 08-16-2022, 05:42 PM
RE: Monty Hall Simulation - by Jack - 08-16-2022, 05:45 PM
RE: Monty Hall Simulation - by SMcNeill - 08-16-2022, 06:02 PM
RE: Monty Hall Simulation - by Kernelpanic - 08-16-2022, 07:40 PM
RE: Monty Hall Simulation - by Pete - 08-17-2022, 01:56 AM



Users browsing this thread: 9 Guest(s)