Question Problems with random functions

krechlich

New member
Joined
Mar 30, 2011
Messages
2
Programming Experience
1-3
Okay, so I'm doing a card game and I'm trying to set that when picturebox (PictureBox1) is clicked, a random card from 13 cards will come visible. Problem is, that when i start the program and click the picturebox, the same card will always come visible. As you can see i've put a button (Button1) which will hide every card. If I press Button2, then Button1 and then the PictureBox1 it will show differend card, but the cards which come visible are always the same.

e.g i press PictureBox1 (which will make card visible), then Button1 (which will make PictureBox1 non-enabled) and then Button2 (which will hide every card and make PictureBox1 pressable). Lets say i pressed the PictureBox1 four times and i got cards: 9, 6, 7, 3, then I close the program, start it again and press PictureBox1 again 4 times and it will show the same cards (9, 6, 7, 3). How can this be done that it will not show the same cards always?

I would really appreciate if someone could give the code, so that always the same pictures won't come visible :D

Thanks in advance :)

Oh, and here's my code...
VB.NET:
Public Class Form1

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

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Button1.Enabled = False
        Button2.Enabled = True
        Label1.Visible = True
        Label2.Visible = True
        Label3.Visible = True
        Label4.Visible = True
        Timer1.Start()
        Label2.Text = "0"
        Label4.Text = "0"
        PictureBox1.Enabled = True

        card1_1.Visible = False
        card1_2.Visible = False
        card1_3.Visible = False
        card1_4.Visible = False
        card1_5.Visible = False
        card1_6.Visible = False
        card1_7.Visible = False
        card1_8.Visible = False
        card1_9.Visible = False
        card1_10.Visible = False
        card1_11.Visible = False
        card1_12.Visible = False
        card1_13.Visible = False
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Button2.Enabled = False
        Button1.Enabled = True
        Timer1.Stop()
        PictureBox1.Enabled = False
    End Sub

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        Label4.Text = Label4.Text + 1
    End Sub

    Private Sub PictureBox1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox1.Click
        Dim Key As Integer
        Key = Int(Rnd() * 13)
Select Key
            Case 1
                card1_1.Visible = True
            Case 2
                card1_2.Visible = True
            Case 3
                card1_3.Visible = True
            Case 4
                card1_4.Visible = True
            Case 5
                card1_5.Visible = True
            Case 6
                card1_6.Visible = True
            Case 7
                card1_7.Visible = True
            Case 8
                card1_8.Visible = True
            Case 9
                card1_9.Visible = True
            Case 10
                card1_10.Visible = True
            Case 11
                card1_11.Visible = True
            Case 12
                card1_12.Visible = True
            Case 13
                card1_13.Visible = True
        End Select
    End Sub
End Class
 
Last edited:
Using the Random class is recommended. Example declaration and instance:
VB.NET:
Private rnd As New Random
getting a random number:
VB.NET:
Select Case rnd.Next(1, 14)
Btw, when you use Rnd function like you do the behaviour you describe is documented and expected.
 
Using the Random class is recommended. Example declaration and instance:
VB.NET:
Private rnd As New Random
getting a random number:
VB.NET:
Select Case rnd.Next(1, 14)
Btw, when you use Rnd function like you do the behaviour you describe is documented and expected.

Thanks mate! I modified your code little, because I couldn't get it work and now it's working. You saved my life mate :p

VB.NET:
Private Sub PictureBox1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox1.Click
        Dim rnd As New Random
        Select Case rnd.Next(1, 13)
            Case 1
                card1_1.Visible = True
            Case 2
                card1_2.Visible = True
            Case 3
                card1_3.Visible = True
            Case 4
                card1_4.Visible = True
            Case 5
                card1_5.Visible = True
            Case 6
                card1_6.Visible = True
            Case 7
                card1_7.Visible = True
            Case 8
                card1_8.Visible = True
            Case 9
                card1_9.Visible = True
            Case 10
                card1_10.Visible = True
            Case 11
                card1_11.Visible = True
            Case 12
                card1_12.Visible = True
            Case 13
                card1_13.Visible = True
        End Select
    End Sub
 
You should do what I suggested and only create one instance of the Random class, and use that to generate all random numbers.
 
Back
Top