Results 1 to 3 of 3

Thread: Need help with randomizing program

  1. #1

    Thread Starter
    New Member
    Join Date
    Oct 2011
    Posts
    2

    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!!

  2. #2
    Freelancer akhileshbc's Avatar
    Join Date
    Jun 2008
    Location
    Trivandrum, Kerala, India
    Posts
    7,652

    Re: Need help with randomizing program

    Hi.. Welcome to the forums

    If you comment out this line:
    Code:
    Application.Exit() 'Closes the appication
    You'll see it in the labelboxes

    If my post was helpful to you, then express your gratitude using Rate this Post.
    And if your problem is SOLVED, then please Mark the Thread as RESOLVED (see it in action - video)
    My system: AMD FX 6100, Gigabyte Motherboard, 8 GB Crossair Vengance, Cooler Master 450W Thunder PSU, 1.4 TB HDD, 18.5" TFT(Wide), Antec V1 Cabinet

    Social Group: VBForums - Developers from India


    Skills: PHP, MySQL, jQuery, VB.Net, Photoshop, CodeIgniter, Bootstrap,...

  3. #3
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106

    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.
    My usual boring signature: Nothing

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