|
-
Apr 2nd, 2003, 05:05 PM
#1
Thread Starter
Lively Member
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:
Private Sub cmdGenerate_Click(Level As Byte)
MsgBox i((10 - 1 + 1) * Rnd + 1)
End Sub
Last edited by notSOMLS1; Apr 3rd, 2003 at 01:01 PM.
-
Apr 2nd, 2003, 05:12 PM
#2
Lively Member
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...
It's also a good idea to Randomize before you call Rnd so you get more "random" numbers each time.
-
Apr 3rd, 2003, 12:23 AM
#3
Fanatic Member
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.
-
Apr 3rd, 2003, 10:30 AM
#4
Thread Starter
Lively Member
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:
FirstNo = Randomize(Int(Rnd * 10) + 1)
-
Apr 3rd, 2003, 10:32 AM
#5
Lively Member
VB Code:
Randomize
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|