Connecting Tech Pros Worldwide Forums | Help | Site Map

Generating Random Numbers in VB6

Moderator
 
Join Date: Oct 2006
Location: Australia
Posts: 7,748
#1   May 4 '07
The VB6 Version

Generating a random number is somewhat different in VB6. It's not for me to say which is better, since I'm only familiar with the VB6 method. But certainly generating a random number (more correctly, a pseudo-random number) is simpler in VB6. You simply call the Rnd() function.

This sample form will do the same thing - each time you click the button, it will display a random number between 1 and 10.

To use this code, start a new project in VB6. Create a new form, and add a command button. Change the name of the command button to "cmdRandom".

If you then double-click the form you will see an "empty" window code template, which should look fairly similar to this...
Expand|Select|Wrap|Line Numbers
  1. Option Explicit
  2.  
  3. Private Sub Form_Load()
  4.  
  5. End Sub
Select the entire window (just press Ctrl-A) then paste this code to replace it...
Expand|Select|Wrap|Line Numbers
  1. Option Explicit
  2. DefLng A-Z
  3.  
  4. Private Sub Form_Load()
  5.   ' At startup, "seed" VB's pseudo-random number generator.
  6.   Randomize
  7. End Sub
  8.  
  9. Private Sub cmdRandom_Click()
  10.   ' Each time the button is clicked, display
  11.   ' a message box showing a random number between 1 and 10.
  12.   MsgBox "The random number generated is: " & Format(RandomNumBetween(1, 10))
  13. End Sub
  14.  
  15. Private Function RandomNumBetween(ByVal LowerLimit As Long, ByVal UpperLimit As Long) As Long
  16.   ' This function returns a pseudo-random number between
  17.   ' the specified limits (inclusive).
  18.   RandomNumBetween = Rnd * (UpperLimit - LowerLimit) + LowerLimit
  19. End Function
For those who prefer it, I'll attach a copy of the Frm file which you can simply add to your project.

There are a couple of things which are perhaps worth mentioning here...
  • From the look of it, this won't apply in VB.Net, but in VB6 it's a good idea to encapsulate your random number generation within a function like this. It's easy to forget the right procedure for generating a random number between two limits, and people tend to end up with numbers that occasionally fall outside the limits, causing program bugs. Setting up a common function like this which you always use in the future prevents this kind of bug.
  • You may have noticed the Option Explicit at the tiop of the code. If your VB installation doesn't insert this automatically, you should pull down to the Tools menu, select [b]Options, go to the Editor tab, and tick the option Require Variable Declaration. A discussion of this option will be posted here soon, but in the meantime, take it from me - this will prevent lots of bugs.
  • Perhaps you also noticed the DefLng A-Z. This simply instructs VB to use Long as the default data type for all variables, if you don’t specify another type. Whenever you are working with whole numbers, the Long data type is generally to be preferred, unless you have a specific reason to avoid it. Being the native data type on a 32 bit processor, it requires less conversion and is therefore slightly faster to process.
Attached Files
File Type: zip Form1.zip (725 Bytes, 521 views)



Closed Thread