# Thread: Dice Simulator question

1. ## Dice Simulator question

Ok I am back with a new question. For my vb class I am supposed to create a dice simulator that will simulate 100 dice rolls and output into a listbox each of the die's number and then the sum of the two. Then, I have 11 labels, and I need a way to go through all 100 sums and count how many of each there are, then output them into the labels. For example dice1 rolls 1, dice2 rolls 4, the sum is 5, therefore that would be 1 for lblSumOf5. I thought maybe I could use an If Else statement, but I can't seem to find a way to make it work.

Here is the code I have so far:

'Program: Dice Simulator
'Form:frmDice
'Date:3/7/2015
'Author:Ethan Forbes
'Purpose: Simulate rolling a pair of dice to demonstrate that some number combinations are more common
'than others.

Option Strict On
Option Explicit On
Public Class frmDice
'=======================================================btnRoll_Click=============================== =================================
Private Sub btnRoll_Click(sender As Object, e As EventArgs) Handles btnRoll.Click
Dim shoDice1 As Short 'Declare shoDice1 as short
Dim shoDice2 As Short 'Declare shoDice2 as short
Dim rand1 As New Random 'Create random number rand1
Dim rand2 As New Random 'Create random number rand2
Dim shoCount As Short = 1 'Declare shoCount as 1
Const LAST_ROLL As Short = 100 'Set constant LAST_ROLL to 100
'Create a For Next statement to simulate 100 rolls of the die and show the outcome and sum of each roll in the lstRoll.
For shoCount = 1 To LAST_ROLL
shoDice1 = CShort(rand1.Next(6) + 1)
shoDice2 = CShort(rand2.Next(6) + 1)
Dim shoSum As Short = shoDice1 + shoDice2
lstRolls.Items.Add("Roll " & shoCount & ": " & shoDice1 & "+" & shoDice2 & "=" & shoSum)
Next

End Sub
'=======================================================btnClear_Click============================== ==================================
Private Sub btnClear_Click(sender As Object, e As EventArgs) Handles btnClear.Click
lstRolls.Items.Clear() 'Clear all Items in the list box
lblSumOf11.Text = "" 'Lines 31-41 clear text from all labels.
lblSumOf12.Text = ""
lblSumOf2.Text = ""
lblSumOf3.Text = ""
lblSumOf4.Text = ""
lblSumOf5.Text = ""
lblSumOf6.Text = ""
lblSumOf7.Text = ""
lblSumOf8.Text = ""
lblSumOf9.Text = ""
lblSumOf10.Text = ""
btnRoll.Focus() 'Set the focus to btnRoll
End Sub
'==========================================================btnExit_Click============================ =================================
Private Sub btnExit_Click(sender As Object, e As EventArgs) Handles btnExit.Click
Me.Close() 'Close the form
End Sub
'=============================================================frmDice_Form========================== ==================================
Private Sub frmDice_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing
'Ask the user if s/he wants to close the form.
If MessageBox.Show("Are you sure?", "Quit Program?", MessageBoxButtons.YesNo, _
MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) = _
Windows.Forms.DialogResult.No Then
e.Cancel = True
End If
End Sub
End Class

2. ## Re: Dice Simulator question

you could use a dictionary like this:

Code:
```Public Class Form1

Private Structure lblFromInteger
Dim value As Integer
Dim lbl As Label
End Structure

Dim d As New Dictionary(Of lblFromInteger, Integer)

Dim r As New Random

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
For x As Integer = 2 To 12
d.Add(New lblFromInteger With {.value = x, .lbl = DirectCast(Me.Controls("lblSumOf" & x.ToString), Label)}, 0)
Next
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim n1 As Integer = r.Next(1, 7)
Dim n2 As Integer = r.Next(1, 7)
ListBox1.Items.Add(n1.ToString & "," & n2.ToString)
Dim item As lblFromInteger = Array.Find(d.Keys.ToArray, Function(l) l.value = n1 + n2)
d(item) += 1
item.lbl.Text = d(item).ToString
End Sub

End Class```

3. ## Re: Dice Simulator question

We haven't technically learned about Arrays or Dictionaries yet so I'm not allowed to use them yet.

4. ## Re: Dice Simulator question

Ok. You need 11 form level Integer variables to store the count for each possible sum.
Only use 1 form level Random Object, or you won't get any variation between your dice rolls.
Use a Select Case block to update the lbSumOf#.Text

5. ## Re: Dice Simulator question

What do you mean just one random object? I've got 2 random right now, one for each die, and it's running correctly.

6. ## Re: Dice Simulator question

Originally Posted by GetOverHere89
What do you mean just one random object? I've got 2 random right now, one for each die, and it's running correctly.
The way you have it the random numbers are likely the same. Here is a short version of what you have

Code:
```    Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
Dim rand1 As New Random 'Create random number rand1
Dim rand2 As New Random 'Create random number rand2

Dim one As Integer = rand1.Next(1, 7)
Dim two As Integer = rand2.Next(1, 7)

Debug.WriteLine(one.ToString & "  " & two.ToString)
End Sub```
The declaration of the random's are seeded with the same number (datetime.now.ticks I think) and the algorithm will return the same number even though there are two random's.

Here is the same example with the random object declared only once.
Code:
```    Dim prng As New Random

Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click

Dim one As Integer = prng.Next(1, 7)
Dim two As Integer = prng.Next(1, 7)

Debug.WriteLine(one.ToString & "  " & two.ToString)
End Sub```

7. ## Re: Dice Simulator question

So there's really nothing wrong with what I have, but I'm using more code then I need to be by attempting to declare it twice when I should just do it once?

8. ## Re: Dice Simulator question

Never mind I see what you're saying now. The output is the same for both, so it's 3+3 or 5+5, etc. Thanks for the help guys.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•

Click Here to Expand Forum to Full Width