Results 1 to 5 of 5

Thread: how to generate a random number??[resolved]

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jan 2003
    Location
    Dallas, TX
    Posts
    70

    Question how to generate a random number??[resolved]

    Lets say I want a number between 1 & 10.
    I tried this code, but it's not working...

    VB Code:
    1. Private Sub cmdGenerate_Click(Level As Byte)
    2.    MsgBox i((10 - 1 + 1) * Rnd + 1)
    3. End Sub
    Last edited by notSOMLS1; Apr 3rd, 2003 at 01:01 PM.

  2. #2
    Lively Member
    Join Date
    Mar 2003
    Location
    Cardiff, UK
    Posts
    67
    Rnd generates a number between 0 and 1, so to generate a number between 1 and 10, we must multiple Rnd by 10. However, if we want an integer number, we must also call Int()

    Now, that might leave us with 0, and also we will never get 10, since Int() just chucks away everything after the decimal point. So in order to get a number between 1 and 10...

    VB Code:
    1. x = Int(Rnd * 10) + 1

    It's also a good idea to Randomize before you call Rnd so you get more "random" numbers each time.

  3. #3
    Fanatic Member
    Join Date
    Oct 1999
    Location
    England
    Posts
    982
    You may know this but just thought I would mention using the RANDOMIZE keyword.
    Put it in your code before using the rnd() function. It ensures you get a different sequence of random numbers every time you call the function otherwise the pattern of numbers will be the same.

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Jan 2003
    Location
    Dallas, TX
    Posts
    70
    Originally posted by davidrobin
    You may know this but just thought I would mention using the RANDOMIZE keyword.
    Put it in your code before using the rnd() function. It ensures you get a different sequence of random numbers every time you call the function otherwise the pattern of numbers will be the same.
    I didn't know that. I was having the problem that the sequence was always the same..
    I tried inserting RANDOMIZE, but can't get it to work..

    Where exactly does it go?

    VB Code:
    1. FirstNo = Randomize(Int(Rnd * 10) + 1)

  5. #5
    Lively Member
    Join Date
    Mar 2003
    Location
    Cardiff, UK
    Posts
    67
    VB Code:
    1. Randomize
    2. FirstNo = Int(Rnd * 10) + 1)

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