i type this:
Private Sub Form_Load()
MsgBox rnd
End Sub
each time i start the program it displays the same exact number. It isn't supposed to do that. And it didn't use to do it either.
Printable View
i type this:
Private Sub Form_Load()
MsgBox rnd
End Sub
each time i start the program it displays the same exact number. It isn't supposed to do that. And it didn't use to do it either.
Try this:VB Code:
Private Sub Form_Load() Randomize Timer MsgBox rnd End Sub
Randomize is a function which initialises VB's random number generator based on an argument (Timer in this case) in some way... :)
it is the same in debug mode since they want you to be able to test the results on any errors with the same number.
rnd random a number between 0 and 1 so if you want to have a random number between 7 and 10 you will need to do :
rnd * 3 + 7
That a little tip :)
Here's a full function:Quote:
Originally posted by DaoK
rnd random a number between 0 and 1 so if you want to have a random number between 7 and 10 you will need to do :
rnd * 3 + 7
That a little tip :)
VB Code:
Public Function RandInt(lowerbound As Integer, upperbound As Integer) RandInt = Int((upperbound - lowerbound + 1) * Rnd + lowerbound End Function
You should add Randomize in the Function
Like I said before, you should only call randomize when you start your program, otherwise you get a severe performace hit.Quote:
Originally posted by DaoK
You should add Randomize in the Function
And it's should be Randomize Time or something like that someone told me to have more change to have a real Random number.
Timer is the default argument for Randomize, so these two do the same thing. Trust me, I'm right, there was a thread a while back if you search for it.
VB Code:
Randomize Randomize Timer
how do u do this if you want the no between -0.1 and 0.1
hi Muds
Rnd returns a number betw 0 and 1 and so u just need to multiply by the required range. U can add or subtract minor decimal roundings as required.
Regards
StuartVB Code:
Private Sub Command1_Click() Randomize Label1 = Format$(Rnd * 0.2 - 0.1, "0.000") End Sub