VB provides a simple little function that lets you generate your own random (or as random as you can get on a PC!) numbers, called Rnd. Below is an example. For more information, click here.
Private Sub cmdRollDice_Click()
Dim intResult As Integer
'// Initializes the random-number generator, otherwise each
time you run your
'// program, the sequence of numbers will be the same
Randomize
intResult = Int((6 * Rnd) + 1) '// Generate random value
between 1 and 6.
MsgBox "Dice: " & intResult '// Display result
End Sub
Private Sub cmdGetRandomNumber_Click()
Dim intResult As Integer
'// Initializes the random-number generator, otherwise each
time you run your
'// program, the sequence of numbers will be the same
Randomize
intResult = Int((100 * Rnd) + 1) '// Generate random value
between 1 and 100.
MsgBox "Random number: " & intResult '// Display result
End Sub
Comments