locked
dice rolling simulation programme behaves differently without msgbox

    Question

  • hi

    i was playing a game at school where pupils had to choose any nine numbers between 2 and 11 to form a bingo card

    two dice were then rolled and if i had that total on my card then i could cross it off

    i won when i got all nine numbers crossed off

    i decided i wanted to simulate this in vb.net to count how many dice rolls it took to get a bingo with different numbers on the cards

    I coded this which works as expected. However, when i take out the line

    MsgBox(

    "Rolled a " & DiceRoll)

    then the function playonce returns ridiculously high answers.

    any help much appreciated - thanks

    ----------------
    form needs nine textboxes names textbox1, textbox2 etc
    and a button named button1
    ----------------
    'CODE STARTS

     

    Public Class Form1
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            TextBox1.Text = "7"
            TextBox2.Text = "7"
            TextBox3.Text = "7"
            TextBox4.Text = "7"
            TextBox5.Text = "8"
            TextBox6.Text = "8"
            TextBox7.Text = "6"
            TextBox8.Text = "6"
            TextBox9.Text = "6"
        End Sub
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    
            'Play the game once and return how many rolls it took for a bingo
    
            MsgBox("It took " & playonce() & " rolls before we got bingo")
    
        End Sub
    
        'returns the number of rolls until BINGO!
        Function playonce()
            playonce = 0
    
            'Read in the players card
    
            Dim PlayingCard(9) As Integer
            PlayingCard(1) = TextBox1.Text
            PlayingCard(2) = TextBox2.Text
            PlayingCard(3) = TextBox3.Text
            PlayingCard(4) = TextBox4.Text
            PlayingCard(5) = TextBox5.Text
            PlayingCard(6) = TextBox6.Text
            PlayingCard(7) = TextBox7.Text
            PlayingCard(8) = TextBox8.Text
            PlayingCard(9) = TextBox9.Text
    
    
            'set the crossedoff flags to 0
    
            Dim CrossedOff(9) As Integer
            For aa = 1 To 9
                CrossedOff(aa) = 0
            Next
    
    
            Dim BINGO As Boolean = False
            Dim DiceRoll As Integer
            Dim tmp As Integer
            Dim RollCount = 0
    
    
            While BINGO = False
    
                'Roll the dice and add one to the roll count
    
                DiceRoll = twodice()
                RollCount = RollCount + 1
                MsgBox("Rolled a " & DiceRoll)
    
    
                For aa As Integer = 1 To 9
    
                    'if the diceroll matches the number on out card and that number hasn't been
                    'crossed off then cross it off and colour the box blue to show we've cross it off
    
                    If DiceRoll = PlayingCard(aa) And CrossedOff(aa) = 0 Then
                        CrossedOff(aa) = 1
                        Call ColourBox(aa)
                        MsgBox("So far we've had " & RollCount & " rolls and just crossed a number off")
                        Exit For
                    End If
    
                Next
                tmp = 0
    
    
    
                'check to see if we have all 9 crossed off
                'If we have then BINGO = true and we can exit the while loop
    
                For bb As Integer = 1 To 9
                    tmp = tmp + CrossedOff(bb)
                Next
                If tmp = 9 Then BINGO = True
    
            End While
    
            playonce = RollCount
    
        End Function
    
        'colours the textbox when number is crossed off
    
        Sub ColourBox(ByVal aa)
    
            If aa = 1 Then TextBox1.BackColor = Color.Azure
            If aa = 2 Then TextBox2.BackColor = Color.Azure
            If aa = 3 Then TextBox3.BackColor = Color.Azure
            If aa = 4 Then TextBox4.BackColor = Color.Azure
            If aa = 5 Then TextBox5.BackColor = Color.Azure
            If aa = 6 Then TextBox6.BackColor = Color.Azure
            If aa = 7 Then TextBox7.BackColor = Color.Azure
            If aa = 8 Then TextBox8.BackColor = Color.Azure
            If aa = 9 Then TextBox9.BackColor = Color.Azure
    
    
        End Sub
    
        Function twodice()
            Dim RandomClass As New Random()
            Dim D1, D2 As Integer
            D1 = RandomClass.Next(1, 7)
            D2 = RandomClass.Next(1, 7)
            twodice = D1 + D2
        End Function
    
    
    End Class
    
    Thursday, October 08, 2009 6:10 PM

Answers

  • Try moving:

    Dim RandomClass As New Random()

    outside the twodice method or declare it as static
    • Marked as answer by Seekaye Thursday, October 08, 2009 6:58 PM
    Thursday, October 08, 2009 6:25 PM

All replies

  • Try moving:

    Dim RandomClass As New Random()

    outside the twodice method or declare it as static
    • Marked as answer by Seekaye Thursday, October 08, 2009 6:58 PM
    Thursday, October 08, 2009 6:25 PM
  • thanks very much - i changed it to

     Static RandomClass As New Random()
            
    and it now works without the msgbox.

    could you please explain what was going on and why the msgbox made the programme work?

    thanks

    Thursday, October 08, 2009 6:47 PM
  • Without the msgbox, you call the function twodice so fast that successive calls to New Random() result in instances with the same seed.  The practical result is that your twodice method generates "many" identical results in a row greatly increasing your counter.

    With the call to msgbox, the seed has "lots" of time to properly increase.

    Once declared as static, RandomClass only needs to be seeded once and things work as you might expect.
    Thursday, October 08, 2009 9:32 PM