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
Printable View
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
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:
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.
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..Quote:
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 tried inserting RANDOMIZE, but can't get it to work..
Where exactly does it go?
VB Code:
FirstNo = Randomize(Int(Rnd * 10) + 1)
VB Code:
Randomize FirstNo = Int(Rnd * 10) + 1)