Rated
Read 46,181 times
Related Categories
Generating Random Numbers
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
James first started writing tutorials on Visual Basic in 1999 whilst starting this website (then known as VB Web). Since then, the site has grown rapidly, and James has written numerous tutorials, articles and reviews on VB, PHP, ASP and C#. In October 2003, James formed the company Developer Fusion Ltd, which owns this website, and also offers various development services. In his spare time, he's a 3rd year undergraduate studying Computer Science in the UK. He's also a Visual Basic MVP.
Comments
|