Library code snippets

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

Comments

  1. 01 Aug 2005 at 22:39
    you mean "click here" link
  2. 13 Apr 2003 at 22:32

    Goes to the very page it's on. Oops?

  3. 01 Jan 1999 at 00:00

    This thread is for discussions of Generating Random Numbers.

Leave a comment

Sign in or Join us (it's free).

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 ...

Related discussion

Related podcasts

  • Christian Beauclair

    14 mai 2008 (�mission #0074) ::.Christian Beauclair: Stratégies de migration VB6 vers .NET Nous discutons avec Christian Beauclair des stratégies de migration VB6 vers .NET. Entre autres, nous discutons comment utiliser le "VB 6 Code Advisor" et le "Interop Forms Toolkit" pour ajouter la puiss...

We'd love to hear what you think! Submit ideas or give us feedback