Noah Mabile
Noah Mabile

Reputation: 1

Random is not being to random

Public Class Form1
Dim num1 As Integer = CInt(Int((10 * Rnd()) + 1))
Dim num2 As Integer = CInt(Int((10 * Rnd()) + 1))



End Sub
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
    TextBox2.Text = num1 & "*" & num2
End Sub

Private Sub TextBox1_TextChanged(sender As System.Object, e As System.EventArgs) Handles TextBox1.TextChanged

End Sub
Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
    If TextBox1.Text = num1 * num2 Then
        Label2.Text = "Correct!!11"
    Else
        Label2.Text = "Incorrect, sorry about that"
    End If
End Sub

End Class

When i run this code, it only generates one question. which is 6*8. If i input 48, it works.But if i click the button again it will not generate another question. It will only generate 6*8. I need it to be able to generate random multiplication questions from 1-10

Upvotes: 0

Views: 111

Answers (2)

Wolves
Wolves

Reputation: 515

If you get 6*8 every time you run the program, including completely stopping and rerunning,then that's really strange and I'm not sure what's up with that.

But if it's just that you're getting the same question every time you click the button, its because you put in a Rnd() when you declared the variables. If you want a new random number each time, you will have to put in some kind of loop where you reassign the variable each time.

Upvotes: 0

p.s.w.g
p.s.w.g

Reputation: 149000

You're only generating num1 and num2 once, when the instance of the form is initialized, so every time you click the button, it's just reusing the same value.

You should generate a new values every time the button is clicked:

Dim num1 As Integer
Dim num2 As Integer

Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
    num1 = CInt(Int((10 * Rnd()) + 1))
    num2 = CInt(Int((10 * Rnd()) + 1))
    TextBox2.Text = num1 & "*" & num2
End Sub

Upvotes: 3

Related Questions