excelvbacoin-flipping

How to simulate a coin flip with While... Wend and Do... Until?


Develop a solution to flip a coin a given amount of times and then print the number of heads and the number of tail. The equation to toss the coin is

Coin = Integer(random*2) + 1

When Coin = 1 the toss is heads, and when Coin = 2 the toss is tails.

Random returns a number between 0 and 1, including 0, but not 1.

Therefore, when random is less than 0.5, Coin will equal 1; and when random is greater than or equal to 0.5 and less than 1, Coin will equal 2.

Structure: While... wend and Do... Until

I've done For Next structure, but I have trouble turning it into those structures.

Dim flips As Integer, heads As Integer, tails As Integer, i As Integer
heads = 0
tails = 0 
flips = InputBox("How many flips?") + 0
For i = 1 To flips
    If (Rnd * 2) + 1 >= 0.5 And (Rnd * 2) < 1 Then
        tails = tails + 1
    Else 
        heads = heads + 1
    End If
Next i 
MsgBox (heads & " heads, " & tails & " tails.")

Solution

  • Loops

    The principle

    ' For
    For i = 1 To flips
        '...
    Next i
    
    ' While
    i = 0 ' by default
    While i < flips
        i = i + 1
        '...
    Wend
    
    ' Do (1)
    i = 0 ' by default
    Do Until i = flips
        i = i + 1
        '...
    Loop
    
    
    ' Do (2)
    i = 0 ' by default
    Do
        i = i + 1
        '...
    Loop Until i = flips
    

    Applied

    Sub flipsFor()
        Dim flips As Integer, heads As Integer, tails As Integer, i As Integer
        heads = 0
        tails = 0
        flips = InputBox("How many flips?") + 0
        ' For
        For i = 1 To flips
            If (Rnd * 2) + 1 >= 0.5 And (Rnd * 2) < 1 Then
                tails = tails + 1
            Else
                heads = heads + 1
            End If
        Next i
        MsgBox (heads & " heads, " & tails & " tails.")
    End Sub
    Sub flipsWhile()
        Dim flips As Integer, heads As Integer, tails As Integer, i As Integer
        heads = 0
        tails = 0
        flips = InputBox("How many flips?") + 0
        ' While
        i = 0 ' by default
        While i < flips
            i = i + 1
            If (Rnd * 2) + 1 >= 0.5 And (Rnd * 2) < 1 Then
                tails = tails + 1
            Else
                heads = heads + 1
            End If
        Wend
        MsgBox (heads & " heads, " & tails & " tails.")
    End Sub
    
    Sub flipsDo1()
        Dim flips As Integer, heads As Integer, tails As Integer, i As Integer
        heads = 0
        tails = 0
        flips = InputBox("How many flips?") + 0
        ' Do (1)
        i = 0 ' by default
        Do Until i = flips
            i = i + 1
            If (Rnd * 2) + 1 >= 0.5 And (Rnd * 2) < 1 Then
                tails = tails + 1
            Else
                heads = heads + 1
            End If
        Loop
        MsgBox (heads & " heads, " & tails & " tails.")
    End Sub
    
    Sub flipsDo2()
        Dim flips As Integer, heads As Integer, tails As Integer, i As Integer
        heads = 0
        tails = 0
        flips = InputBox("How many flips?") + 0
        ' Do (2)
        i = 0 ' by default
        Do
            i = i + 1
            If (Rnd * 2) + 1 >= 0.5 And (Rnd * 2) < 1 Then
                tails = tails + 1
            Else
                heads = heads + 1
            End If
        Loop Until i = flips
        MsgBox (heads & " heads, " & tails & " tails.")
    End Sub