korhan
Board Regular
- Joined
- Nov 6, 2009
- Messages
- 215
Hi everyone,
I have written a code for Monty Hall problem from scratch. I really didn't read anybody else's because it was making everything more confusing. Results are correct. If you switch you win more; however, my code seems a little redundant. I am trying to shorten it and if you are familiar with this simulation please share thoughts and ideas. Anything is greatly appreciated.
I have written a code for Monty Hall problem from scratch. I really didn't read anybody else's because it was making everything more confusing. Results are correct. If you switch you win more; however, my code seems a little redundant. I am trying to shorten it and if you are familiar with this simulation please share thoughts and ideas. Anything is greatly appreciated.
Code:
Option Explicit
Sub Main()
' Declare constants
Const highValue As Integer = 3
Const lowValue As Integer = 1
Const repeat As Long = 10000
' Declare variables
Dim dicDoorsMain As Dictionary
Dim dicDoorsLeft As Dictionary
Dim pickedDoor As Integer
Dim prizeDoor As Integer
Dim noSwitchCase As Long
Dim switchCase As Long
Dim strWinner As String
Dim scenarios As Integer
Dim boolSwitch As Boolean
' Initialize objects
Set dicDoorsMain = New Dictionary
Set dicDoorsLeft = New Dictionary
' Assign values to the object dicDoors
' Repeat this game twice for scenarios
' 1) Switch
' 2) Stay
For scenarios = 1 To 2
switchCase = 0
noSwitchCase = 0
' Scenario one
If scenarios = 1 Then
boolSwitch = False
Else
boolSwitch = True
End If
' Declare a counter variable
' Loop begins here
Dim i As Long
For i = 1 To repeat
' Set strWinner to null string
strWinner = Empty
' Create the dictionary object for main doors (participant's options)
With dicDoorsMain
.Add "Door 1", 1
.Add "Door 2", 2
.Add "Door 3", 3
End With
' Create the dictionary object for doors left( Monty's options)
With dicDoorsLeft
.Add "Door 1", 1
.Add "Door 2", 2
.Add "Door 3", 3
End With
' Pick prize door and participant's door
prizeDoor = Int((highValue - lowValue + 1) * Rnd + 1)
pickedDoor = Int((highValue - lowValue + 1) * Rnd + 1)
' Remove the doors from the possible selections for switch scenarios
With dicDoorsLeft
If .Exists("Door " & prizeDoor) Then: .Remove ("Door " & prizeDoor)
If .Exists("Door " & pickedDoor) Then: .Remove ("Door " & pickedDoor)
End With
' Monty picks a door from possible doors
Dim montyDoor As Integer
' If prizeDoor and pickedDoor are different then Monty has only one choice
If prizeDoor <> pickedDoor Then
montyDoor = dicDoorsLeft.Items(0)
Else
'If prizeDoor and pickedDoor are the same then Monty has two doors to choose from
montyDoor = Int((dicDoorsLeft.Count - 1 + 1) * Rnd + 1)
End If
' Remove Monty's door from possible options of selections
With dicDoorsMain
If .Exists("Door " & montyDoor) Then: .Remove ("Door " & montyDoor)
End With
' Case with no switch
If boolSwitch = False Then
If pickedDoor = prizeDoor Then
noSwitchCase = noSwitchCase + 1
strWinner = "No Switch"
End If
ElseIf boolSwitch Then
' Case switch
With dicDoorsMain
If .Exists("Door " & pickedDoor) Then: .Remove ("Door " & pickedDoor)
If .Keys(0) = "Door " & prizeDoor Then
switchCase = switchCase + 1
strWinner = "Switch"
End If
End With
End If
' Erase dictionary objects
dicDoorsLeft.RemoveAll
dicDoorsMain.RemoveAll
Next i
' Print the results
If boolSwitch = False Then
Debug.Print "No switch " & noSwitchCase / repeat
ElseIf boolSwitch Then
Debug.Print "Switch " & switchCase / repeat
End If
Next scenarios
End Sub