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

You might also like...

Comments

James Crowley James first started this website when learning Visual Basic back in 1999 whilst studying his GCSEs. The site grew steadily over the years while being run as a hobby - to a regular monthly audience ...

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“Linux is only free if your time has no value” - Jamie Zawinski