Need help with randomizing program
Hey i am making a program which generates a random number between 1 and 10 and when 7 appears it is suppose to tell you how many tries it took to get the number 7 and then end the application. This is the code I have used:
'Declaration of variables
Dim Number As Integer
Dim Random As Integer
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Randomize() 'Randomizes number
End Sub
Private Sub btnNumber_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNumber.Click
Do
Number = Int(10 * Rnd() + 1) 'Randomizes the variable
lblNumber.Text = Number 'Displays number in label
Random = Random + 1 'Increases count by an increment of 1
lblRandom.Text = Random 'Displays count in label
Loop Until Number = 7 'End loop when variable is 7
If Number = 7 Then 'If number is 7
Random = 0 'Change value of variable
MsgBox("Congratulations, it took you " & lblRandom.Text & " tries to get the number 7!") 'Displays a message box which displays the number of tries it took you to get the number 7
Application.Exit() 'Closes the appication
End If
End Sub
End Class
This code only generates the number 7 and exits the application, each time i click the random button but i want it to show the other number it randomizes too for example 1 2 3 4 .. etc, if u dont understand, please try it, but im trying to say, when i clikc random it just says number 7 (does randomizing in background) and tells you how much tries it took but i want it to show the other number it randomized also and when 7 appears, then exit the program
PLEASE HELP!!
Re: Need help with randomizing program
Hi.. Welcome to the forums :wave:
If you comment out this line:
Code:
Application.Exit() 'Closes the appication
You'll see it in the labelboxes :thumb:
Re: Need help with randomizing program
Get rid of that call to Randomize, then get rid of the call to Rnd. That's all VB6 style random numbers, which have issues. In .NET, you should be using a Random object. Create one at form scope, then call the .Next method to get a random number (there are a couple overloads for that one depending on the range of numbers you want).
Naturally, that will make it fairly necessary to rename your Random variable, since Random is a class type.