How to resolve the algorithm Monty Hall problem step by step in the FreeBASIC programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Monty Hall problem step by step in the FreeBASIC programming language

Table of Contents

Problem Statement

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. The car and the goats were placed randomly behind the doors before the show.

After you have chosen a door, the door remains closed for the time being. The game show host, Monty Hall, who knows what is behind the doors, now has to open one of the two remaining doors, and the door he opens must have a goat behind it. If both remaining doors have goats behind them, he chooses one randomly. After Monty Hall opens a door with a goat, he will ask you to decide whether you want to stay with your first choice or to switch to the last remaining door. Imagine that you chose Door 1 and the host opens Door 3, which has a goat. He then asks you "Do you want to switch to Door Number 2?"

Is it to your advantage to change your choice?

The player may initially choose any of the three doors (not just Door 1), that the host opens a different door revealing a goat (not necessarily Door 3), and that he gives the player a second choice between the two remaining unopened doors.

Run random simulations of the Monty Hall game. Show the effects of a strategy of the contestant always keeping his first guess so it can be contrasted with the strategy of the contestant always switching his guess. Simulate at least a thousand games using three doors for each strategy and show the results in such a way as to make it easy to compare the effects of each strategy.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Monty Hall problem step by step in the FreeBASIC programming language

Source code in the freebasic programming language

' version 19-01-2019
' compile with: fbc -s console

Const As Integer max = 1000000
Randomize Timer

Dim As UInteger i, car_door, chosen_door, montys_door, stay, switch

For i = 1 To max
    car_door = Fix(Rnd * 3) + 1
    chosen_door = Fix(Rnd * 3) + 1
    If car_door <> chosen_door Then
        montys_door = 6 - car_door - chosen_door
    Else
        Do
          montys_door = Fix(Rnd * 3) + 1
        Loop Until montys_door <> car_door
    End If
    'Print car_door,chosen_door,montys_door
    ' stay
    If car_door = chosen_door Then stay += 1
    ' switch
    If car_door = 6 - montys_door - chosen_door Then switch +=1
Next

Print Using "If you stick to your choice, you have a ##.## percent" _
                                         + " chance to win"; stay / max * 100
Print Using "If you switched, you have a ##.## percent chance to win"; _
                                                           switch / max * 100

' empty keyboard buffer
While InKey <> "" : Wend
Print : Print "hit any key to end program"
Sleep
End

  

You may also check:How to resolve the algorithm Date format step by step in the Apex programming language
You may also check:How to resolve the algorithm MD4 step by step in the Ada programming language
You may also check:How to resolve the algorithm Greatest element of a list step by step in the REBOL programming language
You may also check:How to resolve the algorithm Grayscale image step by step in the Liberty BASIC programming language
You may also check:How to resolve the algorithm Thue-Morse step by step in the NewLISP programming language