Results 1 to 4 of 4

Thread: Random Number

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jan 2000
    Location
    WALES UK
    Posts
    31
    Can any body give me the code that will create a six digit random number....and place it in a text box

    The number must be random everytime the button is clicked??

    I code some stuff but it says overflow!!!

    Cheers

    Rhys

  2. #2
    Fanatic Member
    Join Date
    Mar 2000
    Location
    That posh bit of England known as Buckinghamshire
    Posts
    658
    To get a random number between two numbers use the formula

    Code:
    Int((upperbound - lowerbound + 1) * Rnd + lowerbound)
    in this case you want a random six digit number so the upper bound is 999999 and the lower bound is 100000.

    Code:
    Text1.Text = Int((999999 - 100000 + 1) * Rnd + 100000)
    Iain, thats with an i by the way!

  3. #3
    Frenzied Member HarryW's Avatar
    Join Date
    Jan 2000
    Location
    Heiho no michi
    Posts
    1,827
    I guess you're using short integers or something. Maybe try longs? If you're already doing that, then you could make 6 random numbers between 0-9 and then stick em together, and put the string in a text box. Something like this (can't remember some of it and I don't have VB here at work):

    Code:
    dim numstring as string
    dim numbers(5) as byte
    numstring = str(int(rnd * 9 + 1)) ' need at least 1 for a 6 digit number
    for x = 2 to 6
      numbers(x) = int(rnd * 10) 'Check syntax, I can't quite remember
      numstring = numstring & str(numbers(x))
    next x
    
    text1.text = numstring
    Dammit I'm rusty at this. We do C and Pascal at uni, and I haven't done this kinda thing for a while. Can't guarantee those functions are right, but try it maybe?

    Harry.

    "From one thing, know ten thousand things."

  4. #4

    Thread Starter
    Junior Member
    Join Date
    Jan 2000
    Location
    WALES UK
    Posts
    31
    thanx I have done it now...I put the Randomize function in front of the first reply and now every time the form loads the number is different...

    cheers

    Rhys

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