Youssef haddara - 11 months ago 39

Vb.net Question

What I am trying to do:

I want to loop infinitely until I get 5 tails in a row. I also want to show the number of attempts it took to be able to achieve 5 tails in a row

Here is what I have:

`Dim number = rand.Next(1, 3)`

If number = 1 Then

RichTextBox1.Text += "Tails" & vbNewLine

ElseIf number = 2 Then

RichTextBox1.Text += "Heads" & vbNewLine

End If

number = rand.Next(1, 3)

I haven't been able to figure it out. Thanks in advance for any help.

Answer Source

You need a counter variable that stores how often you already had tails. `While tailCount < 5`

will break this loop if you have found 5. Another counter will count the total attempts:

```
Dim rand As New Random()
Dim maxTailCount = 5
Dim attempts = 0
Dim tailCount = 0
While tailCount < maxTailCount
attempts += 1
Dim number = rand.Next(1, 3)
If number = 1 Then
tailCount += 1
Else
tailCount = 0 ' because they must be "in a row"
End If
End While
Console.WriteLine("Found {0} tails, total attempts: {1}", maxTailCount, attempts)
```