Results 1 to 10 of 10

Thread: Random Numbers 1 -50

  1. #1

    Thread Starter
    New Member
    Join Date
    Nov 2007
    Posts
    5

    Question Random Numbers 1 -50

    How do I code an application that allow a user 10 chances to guess a random number from 1-50 and each time it's incorrect it display "Try Again" or guess correctly "Congradulations"
    After 10 tries, it display the random number.

    I know this is very simple, but I am a stylist who is just learning this stuff.

    Thanks,

    J

  2. #2
    Frenzied Member
    Join Date
    Mar 2006
    Location
    Pennsylvania
    Posts
    1,069

    Re: Random Numbers 1 -50

    Well, are you doing this in a console application or windows application? What are you using for input? I guess it would be safe to assume that you are using VB, but which version? 2003? 2005? 2008? How much experience do you have? Can you write if statement, loops, or handle input and send output?

    We're not doing this for you

  3. #3

    Thread Starter
    New Member
    Join Date
    Nov 2007
    Posts
    5

    Re: Random Numbers 1 -50

    Windows application and for input I am using an inputbox. Yes it is VB 2005, I have little experience and yes I can write if statments, but I get very confused using the loop statements.
    Here is what I have so far:
    Dim Guess As String
    Dim number As Double

    Guess = InputBox("Guess the Number")

    If number Like "[1-50]" Then
    MessageBox.Show("Congradulations")
    Else
    If number > 50 Then
    MessageBox.Show("Lower")
    Else
    End If

  4. #4
    Frenzied Member MaximilianMayrhofer's Avatar
    Join Date
    Aug 2007
    Location
    IM IN YR LOOP
    Posts
    2,001

    Re: Random Numbers 1 -50

    The first thing the code should do on run is create a string and assign it a random number for a value. Then every time a guess is made, it will be compared to the number in the string. Your structure might be:

    Dim some string as (create a random number here)
    Dim something else to flag when the correct number is chosen

    Do while something else
    --- Input Box code and results ---
    Loop
    Last edited by MaximilianMayrhofer; Nov 24th, 2007 at 10:57 AM.

  5. #5

    Thread Starter
    New Member
    Join Date
    Nov 2007
    Posts
    5

    Re: Random Numbers 1 -50

    So,
    I will code the 10 numbers that will be used as the random numbers?

  6. #6
    Frenzied Member MaximilianMayrhofer's Avatar
    Join Date
    Aug 2007
    Location
    IM IN YR LOOP
    Posts
    2,001

    Re: Random Numbers 1 -50

    You only need one random number.. and then let them keep guessing it..

    If you want to use an input box, then you'll use the code structure I showed above.

    Another option is just to have an input text box on a form and a submit button that tells the program to check whether the number in the textbox is correct. In that case, you'll simply have a piece of code in the on-click event of the button that checks whether the number is correct, and you'll need another variable that keeps track of the number of tries.

  7. #7

    Thread Starter
    New Member
    Join Date
    Nov 2007
    Posts
    5

    Re: Random Numbers 1 -50

    Ok, you confused me even more...., this random number is generated by the computer and the user have only ten tries to guess the correct number.
    So, do I use to IF Then statement or the loop statement for this??

  8. #8

    Thread Starter
    New Member
    Join Date
    Nov 2007
    Posts
    5

    Re: Random Numbers 1 -50

    Dim randomnum1 As Integer
    Dim RandomGenerator As New Random

    If Me.TextBox1.Text Then
    randomnum1 = RandomGenerator.Next(1, 50)

  9. #9
    Frenzied Member
    Join Date
    Mar 2005
    Location
    Sector 001
    Posts
    1,577

    Re: Random Numbers 1 -50

    One thing at a time. First try generating the random number. Make it visible so that you can monitor how your coded logic behaves. Afterwards you'll make that number hidden (it'll be stored only in memory).

    So see if you can use a button to generate a random number and show that number in a label. Note that RandomGenerator.Next(1, 50) will never generate 50 - the first integer is the inclusive lower bound, the second integer is the exclusive upper bound of the range.
    VB 2005, Win Xp Pro sp2

  10. #10
    Frenzied Member MaximilianMayrhofer's Avatar
    Join Date
    Aug 2007
    Location
    IM IN YR LOOP
    Posts
    2,001

    Re: Random Numbers 1 -50

    Just try this so you get an idea. Put a label on your form and put this into the click event of a button on your form:

    vb.net Code:
    1. Dim rndnumber as Integer
    2. dim rndgenerator as New Random
    3.  
    4. rndnumber = rndgenerator.Next(1,51)
    5. Label1.Text = rndnumber.ToString

    Every time you click the button, you should get a random number between 1 and 50 in your label.

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