1. This forum is in read-only mode.

Visual Basic coding problem (coin game)

Discussion in 'General Discussion' started by zeldafan12, Jan 5, 2011.

  1. zeldafan12

    zeldafan12 Well-Known Member

    my assignment is to create a coin game and I am a little stuck. This is my instructions put in a spoiler.
    Coin Toss Program – Programming portion of repetition test
    Use a random number generator to simulate a coin toss.
    First use an input box to ask the user for the number of times the coin is to be tossed.
    Next set up a loop to execute that number of times.
    Each time you enter loop, use the random number next method to generate a random number.
    Assume that a random value of 0 is considered a Heads and a value of 1 is a Tails.
    So, if the random value is 0 add one to an accumulator named intNumHeads.
    If the random value is 1 add one to intNumTails.
    In addition, display in a list box the word “Heads” if the value is 0 or “Tails” if the value is a 1 for each iteration of the loop. (Hint: use a variable named strCoinToss)
    When the loop has finished, output the number of heads and the number of tails in appropriate labels on the form.
    Clear the list box and labels before the executing the program again.

    And I will insert my code in one sec in another spoiler
    (adobe acrobat is being weird about copy/paste on my laptop. think its a bug. So I had to go on my desktop to actually be able to copy the prompt but the coding is on my laptop.)

    Ok here is my coding
    Option Explicit On
    Option Strict On
    Option Infer Off

    Public Class mainForm

    Private Sub exitButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles exitButton.Click
    Me.Close()
    End Sub

    Private Sub calcButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles calcButton.Click

    'declare variables
    Dim number As Integer = 0
    Dim count As Integer
    Dim tailscount As Integer
    Dim headscount As Integer
    Dim randomGenerator As New Random
    Dim inputArea As Object
    Dim message As String
    Dim title As String
    Dim coinside As Integer = randomGenerator.Next(0, 2)
    title = "Coin Toss"
    message = "Enter number of coins you want to flip"


    inputArea = InputBox(message, title, CStr(count))

    'determine if heads or tails are flipped
    Do Until number > count
    If coinside = 0 Then
    headscount = headscount + 1
    Else
    tailscount = tailscount + 1

    End If
    number = number + 1
    Loop

    headsLabel.Text = headscount.ToString
    tailsLabel.Text = tailscount.ToString

    End Sub
    End Class
     
  2. Loonylion

    Loonylion Administrator Staff Member

    for a start, why is only one variable initialised to zero?

    secondly, if I remember right, the code for a randomiser is either rand or rnd, but its a long time since I've used VB. I've certainly never seen RandomGenerator.next syntax. new random also sounds like a user defined function, it's too ambiguous to be a built in one.

    also at no point are the output labels cleared.
     
  3. zeldafan12

    zeldafan12 Well-Known Member

    new random is a built in function i never defined it, that is just how my teacher has shown us how to make a random generator
    im still pretty new
    Post Merge: [time]1294192988[/time]
    do I need another one to be zero?