Results 1 to 8 of 8

Thread: Random numbers and additional characters

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 1999
    Location
    Glasgow,Scotland
    Posts
    281

    I have two short queries. I tried a search on the VB database, but didn't have much success.

    The following code gives me a random number between 0 and 10 in my textbox:

    Private Sub Form_Load()
    Randomize
    Text1 = Int(Rnd * 10)
    End sub

    How can I exclude the number zero from appearing?

    Secondly, I want to add the letter 'X' onto the number that appears in the text box. I thought it was simply a case of:

    Label2.caption = Int(Rnd * 10) + "X".

    But it didn't work.

    Any help on these issues would be greatly appreciated.

  2. #2
    Junior Member
    Join Date
    Mar 2000
    Posts
    29
    This should give you no zero:

    Private Sub Form_Load()
    Randomize
    Text1 = Int(Rnd * 9) + 1
    End sub

    And this should give you 9X, 3X etc

    Label2.caption = Int(Rnd * 10) & "X"



  3. #3
    Hyperactive Member onerrorgoto's Avatar
    Join Date
    Aug 1999
    Location
    Sweden
    Posts
    330

    Almost right browner

    Sorry browner, you are right in all points but one

    You shall use Text1 = Int(Rnd * 10) + 1 since 10 is how many numbers you want the rnd to go between.
    If you use (rnd*9)+1 you will only get #1-9
    but (rnd*10)+1 will give you #1-10

    good luck
    Onerrorgoto

    Dont be to optimistic, the light at the end of the tunnel might be a train

  4. #4
    Junior Member
    Join Date
    Mar 2000
    Posts
    29
    How right you are. So that means Int(Rnd * 10)
    actually returns one of 11 random numbers. I wish VB was less irregular in it's use of 0 or 1 as the staring point in arrays, counts, string functions, etc, etc, etc. But anyway....

  5. #5
    Frenzied Member Mark Sreeves's Avatar
    Join Date
    Nov 1999
    Location
    UK
    Posts
    1,845
    browner, theres nothing irregular about Rnd() it produces a single between 0 and 1

    int() of a number between 0 and 1 will ALWAYS be 0!

    Mark
    -------------------

  6. #6
    Junior Member
    Join Date
    Mar 2000
    Posts
    29
    Alright - that is fair enough - but my point was that in C or other languages you are never in doubt that EVERYTHING runs from zero to...... However in VB some things are not so

    i.e.

    MsgBox Mid("HELLO", 0, 1) = an error whereas
    MsgBox List1.List(0) = not an error

    so if you have 10 characters in a string you have to do a loop from 1 to 10 to access them, however to access ten strings in a listbox you have to go from 0 to 9, to access 10 elements. In an array, you have a choice depending on your option base etc etc etc. So it is never immediately obvious that you start at 1 or 0. Ahh man - I am just getting myself in trouble here... leave me alone. I know that C uses arrays for strings and option base is optional and everything else, but still it is a bit confusing in VB.

    Phew

  7. #7

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 1999
    Location
    Glasgow,Scotland
    Posts
    281
    Thanks a lot, lads.

    I'm never too sure of that rnd stuff myself. I had no excuses for that & "x" instead of + "x", though - I should have figured it out a mile away.

    Thanks.

  8. #8
    Frenzied Member Mark Sreeves's Avatar
    Join Date
    Nov 1999
    Location
    UK
    Posts
    1,845
    Browner,

    Alright - that is fair enough - but my point was that in C or other languages you are never in doubt that EVERYTHING runs from zero to...... However in VB some things are not so
    Yeah, I agree totally

    Listboxes run from 0
    listViews run from 1

    where's the logic in that??
    Mark
    -------------------

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