Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

Visual Basic 2010 Dice Simulator Help

$
0
0
My assignment is to create a dice simulator program that will roll a pair of dice 100 times and calculate the mean and standard deviation of the rolls. The application should also show a table that shows each possible roll of the dice and its frequency of occurrence in the simulation in a listbox. In other words, the table shows a roll of 3, a roll of 4, and so on up to 12. I have most of my code written and I've gone over it a million times and I feel like I might just be missing something simple because I've been staring at it for so long. When I run the program it doesn't show any errors it just freezes up, which it only usually does when I have something very wrong. Any help would be appreciated!
I have the text boxes disabled until the button is clicked because that was also part of the assignment, in case anyone was wondering.

 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        txtDeviation.Enabled = True
        txtMean.Enabled = True
        Dim die1, die2 As Integer
        Dim dicesum As Integer
        Dim twos, threes, fours, fives, sixes, sevens, eights, nines, tens, elevens, twelves As Integer
        Dim numberofrolls As Integer
        Dim randomnum As New Random

        Do
            die1 = randomnum.Next(1, 7)
            die2 = randomnum.Next(1, 7)
            numberofrolls += 1
        Loop Until (numberofrolls = 100)

        If die1 + die2 = 2 Then
            twos += 1
        End If
        If die1 + die2 = 3 Then
            threes += 1
        End If
        If die1 + die2 = 4 Then
            fours += 1
        End If
        If die1 + die2 = 5 Then
            fives += 1
        End If
        If die1 + die2 = 6 Then
            sixes += 1
        End If
        If die1 + die2 = 7 Then
            sevens += 1
        End If
        If die1 + die2 = 8 Then
            eights += 1
        End If
        If die1 + die2 = 9 Then
            nines += 1
        End If
        If die1 + die2 = 10 Then
            tens += 1
        End If
        If die1 + die2 = 11 Then
            elevens += 1
        End If
        If die1 + die2 = 12 Then
            twelves += 1
        End If

        ListBox1.Items.Add("Roll     Frequency")
        ListBox1.Items.Add("=====================")
        ListBox1.Items.Add("2     " & twos)
        ListBox1.Items.Add("3     " & threes)
        ListBox1.Items.Add("4     " & fours)
        ListBox1.Items.Add("5     " & fives)
        ListBox1.Items.Add("6     " & sixes)
        ListBox1.Items.Add("7     " & sevens)
        ListBox1.Items.Add("8     " & eights)
        ListBox1.Items.Add("9     " & nines)
        ListBox1.Items.Add("10     " & tens)
        ListBox1.Items.Add("11     " & elevens)
        ListBox1.Items.Add("12     " & twelves)

        dicesum = die1 + die2
        txtMean.Text = FormatNumber(dicesum / 100, 2)

    End Sub
End Class


Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>