## Some more Monte Carlo experimentation

Disclaimer: I do not in any way, shape, or form condone gambling. This posting is for entertainment purposes only.

I saw this roulette story on The Daily WTF, and thought that it merited some good ole fashioned brute force computing:

Knocking Me Off The Perch

So here is the VB.NET console application code:

```Module Module1

Dim rand As Random

Dim tableResults() As List(Of String)
Dim theWheel() As String = {"G00", "B2", "R14", "B35", "R23", _
"B4", "R16", "B33", "R21", "B6", _
"R18", "B31", "R19", "B8", "R12", _
"B29", "R25", "B10", "R27", _
"G0", "R1", "B13", "R36", "B24", _
"R3", "B15", "R34", "B22", "R5", _
"B17", "R32", "B20", "R7", "B11", _
"R30", "B26", "R9", "B28"}
Dim wheelCount As Integer
Dim tableBetAmount() As Integer
Dim tableBetColor() As String
Dim tableLostLastTime() As String

Dim tables, stake, bet, matches, goal, attempts As Integer
Dim cancelBetAfterAWin As String

Function GetASpin() As String

Return theWheel(rand.Next(0, wheelCount))

End Function

Function GetABet(Optional ByVal bigBet As Boolean = False) As Integer

Dim theBet As Integer

If bigBet Then
theBet = bet * 1.5
Else
theBet = bet
End If

If theBet > stake Then theBet = stake

stake = stake - theBet

Return theBet

End Function

Sub Main()

Dim i, j As Integer
Dim s As String

rand = New Random

Console.WriteLine("The Perch")

Console.Write("How many tables to watch? (4) ")
If tables = 0 Then tables = 4
ReDim tableResults(tables)
ReDim tableBetAmount(tables)
ReDim tableBetColor(tables)
ReDim tableLostLastTime(tables)

Dim stakeSave As Integer

If stakeSave = 0 Then stakeSave = 10

Console.Write("How many money to bet? (10) ")
If bet = 0 Then bet = 10

Console.Write("How many times to hit a color before betting? (4) ")
If matches = 0 Then matches = 4

Console.Write("What is your goal amount? (400) ")
If goal = 0 Then goal = 400

Console.Write("How many times to run the simulation? (1000) ")
If attempts = 0 Then attempts = 1000

Console.Write("Cancel your bet after a win? (Y) ")
If s = "" Then s = "Y"
cancelBetAfterAWin = (s = "Y")

Dim madeGoal As Integer = 0

wheelCount = theWheel.Count

Dim currAttempt As Integer
Dim ctr As Integer
Dim theSpin As String
Dim spinsSeen As Integer
Dim result As String
For currAttempt = 1 To attempts
' initialize the table arrays
stake = stakeSave
For i = 0 To tables - 1
tableResults(i) = New List(Of String)
tableBetAmount(i) = 0
tableLostLastTime(i) = ""
Next
' initialize the lists
For j = 1 To matches
s = String.Format("{0,5}", j.ToString)
For i = 0 To tables - 1
theSpin = GetASpin()
s = s + String.Format("{0,5}{1,-5}", "", theSpin)
Next
s = s + String.Format("{0,5}", stake.ToString)
Console.WriteLine(s)
Next
spinsSeen = matches
' loop
While (stake > 0 And stake < goal)
For i = 0 To tables - 1
If tableLostLastTime(i) <> "" Then
' see if this is the 1st time losing
If tableBetAmount(i) = bet Then
' we lost last time out, automatically bet 1.5x
tableBetAmount(i) = GetABet(True)
tableBetColor(i) = tableLostLastTime(i)
Else
' this is the 2nd time losing in a row, so stop betting
tableBetAmount(i) = 0
tableBetColor(i) = ""
tableLostLastTime(i) = ""
End If
Else
' check to see if the colors match up
ctr = 0
For Each r In tableResults(i)
If Left(r, 1) = "B" Then ctr = ctr + 1
Next
If ctr = matches Then
' it's coming up black, bet on red
tableBetAmount(i) = GetABet()
tableBetColor(i) = "R"
tableLostLastTime(i) = ""
End If
ctr = 0
For Each r In tableResults(i)
If Left(r, 1) = "R" Then ctr = ctr + 1
Next
If ctr = matches Then
' it's coming up red, bet on black
tableBetAmount(i) = GetABet()
tableBetColor(i) = "B"
tableLostLastTime(i) = ""
End If
End If
Next
' now spin for each of the tables
spinsSeen = spinsSeen + 1
s = String.Format("{0,5}", spinsSeen.ToString)
For i = 0 To tables - 1
' pop off the oldest spin for each table
tableResults(i).RemoveAt(0)
' get a new spin for each table
theSpin = GetASpin()
If tableBetAmount(i) = 0 Then
s = s + String.Format("{0,5}{1,-5}", "", theSpin)
Else
s = s + String.Format("{0,5}{1,-5}", _
tableBetAmount(i).ToString + _
tableBetColor(i) + ">", theSpin)
End If
' check for bets
If tableBetColor(i) <> "" Then
If Left(tableBetColor(i), 1) = Left(theSpin, 1) Then
' winner
stake = stake + tableBetAmount(i) * 2
tableLostLastTime(i) = ""
If cancelBetAfterAWin Then
tableBetAmount(i) = 0
tableBetColor(i) = ""
End If
Else
tableLostLastTime(i) = tableBetColor(i)
End If
End If
Next
s = s + String.Format("{0,5}", stake.ToString)
Console.WriteLine(s)
End While
If stake = 0 Then
result = "Busted"
Else
result = "Won \$" + stake.ToString
End If
Console.WriteLine("Spins seen: " + spinsSeen.ToString + _
"; Result: " + result)
Next

Console.WriteLine()
Console.WriteLine("Total simulations: " + attempts.ToString)