Skip to content

Instantly share code, notes, and snippets.

@jda0
Created December 12, 2013 17:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jda0/7932258 to your computer and use it in GitHub Desktop.
Save jda0/7932258 to your computer and use it in GitHub Desktop.
Public Class Form1
' Form Controls: Label Label1, TextBox TxtLimit, Button BtnExe, ListBox LstItems, Label LblInfo
Dim rand As Random
Dim count, coins, flip, tails As Integer
Dim result() As String
Dim done As Boolean
Dim z As Date
Dim o As String
Private Sub BtnExe_Click(sender As System.Object, e As System.EventArgs) Handles BtnExe.Click
LblInfo.Text = "Running..."
LblInfo.Refresh()
z = Date.UtcNow()
'Try
coins = CInt(TxtLimit.Text)
count = 0
tails = 0
Dim flips(coins) As String
ReDim result(0)
LstItems.Items.Clear()
LstItems.Items.Add("")
done = False
Do
LblInfo.Text = "Running... - Flip " + CStr(coins) + Chr(215) + CStr(count / coins)
LblInfo.Refresh()
For i = 0 To coins - 1
flip = rand.Next(2)
flips(i) = GetCoinState(flip)
tails += flip
count += 1
Next
done = True
For i As Integer = 1 To coins - 1
If flips(i) <> flips(i - 1) Then
done = False
Exit For
End If
Next
result(result.Length - 1) = Join(flips, vbTab)
If result.Length < 1000 Then
ReDim Preserve result(result.Length)
Else
result.Reverse()
Array.Resize(result, 999)
result.Reverse()
End If
Loop Until done = True
For i As Integer = 0 To result.Length - 1
If result(i) IsNot Nothing Then LstItems.Items.Add(result(i))
Next
'Catch ex As Exception
' LblInfo.Text = "Error: " + ex.Message
' Exit Sub
'End Try
LstItems.SelectedIndex = LstItems.Items.Count - 1
LblInfo.Text = "Finished in " + CStr((Date.UtcNow - z).TotalMilliseconds Or 0) + "ms - " + CStr(coins) + Chr(215) + CStr(count / coins) + " Flips - " + CStr(count - tails) + " Heads and " + CStr(tails) + " Tails"
End Sub
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
rand = New Random()
End Sub
Function GetCoinState(_state As Integer) As String
If _state = 0 Then
Return "Heads"
Else
Return "Tails"
End If
End Function
End Class
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment