Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here


Visual Basic (Microsoft) Versions 5/6 FAQ


Random numbers by JESTAR
Posted: 18 Feb 03 (Edited 18 Feb 03)

This FAQ will:

1) Demonstrate how to generate a single random number...
2) Show you how to generate a set of random numbers...

... between 2 given values.

You MUST initialize the Random Number Generator BEFORE selecting the random numbers. In your Form_Load() event, simply put the word Randomize, thus:

Private Sub Form_Load()
End Sub

Generate a single random number between 1 and x

Dim MyValue as Integer

MyValue = Int((10 * Rnd) + 1)   ' Generate random value between 1 and 10.

Generate a random number between x and y
Dim MyValue as Integer

intNumber = Int((100 * Rnd) + 150)    ' Generate a random number between 150 (x) and 250 (y)

{Set x (the 150 above) as the lowest number. Set y (the 100 above) to the highest number - x.)

Generate x random numbers between 1 and x,  store in an array

Dim MyValue(9) as Integer
Dim I as Integer

For I = 0 To 9
    MyValue(I) = Int((100 * Rnd) + 1)

This will generate 10 random numbers, store them in an array (MyValue), with the numbers between 1 and 100.

Generate x random numbers between y and z, store in an array

Dim MyValue(5) as Integer
Dim I as Integer

For I = 0 To 5
    MyValue(I) = Int((70 * Rnd) + 30)

(This will generate 6 random numbers between 30 and 70, then store them in the MyValue array.)

Back to Visual Basic (Microsoft) Versions 5/6 FAQ Index
Back to Visual Basic (Microsoft) Versions 5/6 Forum

My Archive

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close