Advertisement
MrMusAddict

Doors

Aug 17th, 2016
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Sub doors()
  2.  
  3. ''''expected vs actual result: http://i.imgur.com/uEA2AZj.png
  4. ''''run as a VBA Macro in Excel
  5.  
  6. Dim dCount As Long 'door count
  7. Dim car As Long 'used to randomly pick which door has the car
  8. Dim choice As Long 'used to pick which door the contestant picks
  9.  
  10. Dim itr As Long 'number of iterations
  11.  
  12. Dim wins As Double 'number of wins
  13.  
  14. itr = 100000
  15. dCount = 3
  16.  
  17. wins = 0
  18.  
  19. For i = 1 To itr
  20.  
  21.     'rnd double between 0 and 1, * door count, add 0.5 to account for rounding.
  22.    car = Round((Math.Rnd * dCount) + 0.5, 0)
  23.     choice = Round((Math.Rnd * dCount) + 0.5, 0)
  24.    
  25.    
  26.     'If the door you picked "choice" is not the car,
  27.    'then the host has shown that the remaining door *is* the car.
  28.    'Therefore, by switching you win.
  29.    If car <> choice Then
  30.         wins = wins + 1 'increase wins
  31.    End If
  32.    
  33.    
  34.     'if the door you picked "choice" is the car,
  35.    'then by switching you will lose.
  36.    'Nothing done since winrate is not increased.
  37.    
  38. Next i
  39.  
  40. MsgBox "Expected: " & Round((dCount - 1) / dCount * 100, 2) & "%" _
  41. & vbNewLine & "Actual: " & Round(wins / itr * 100, 2) & "%"
  42.  
  43. End Sub
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement