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...[CODE=vb]Option Explicit
Private Sub Form_Load()
End Sub[/CODE]
Select the entire window (just press Ctrl-A) then paste this code to replace it...[CODE=vb]Option Explicit
DefLng A-Z
Private Sub Form_Load()
' At startup, "seed" VB's pseudo-random number generator.
Randomize
End Sub
Private Sub cmdRandom_Click ()
' Each time the button is clicked, display
' a message box showing a random number between 1 and 10.
MsgBox "The random number generated is: " & Format(RandomNu mBetween(1, 10))
End Sub
Private Function RandomNumBetwee n(ByVal LowerLimit As Long, ByVal UpperLimit As Long) As Long
' This function returns a pseudo-random number between
' the specified limits (inclusive).
RandomNumBetwee n = Rnd * (UpperLimit - LowerLimit) + LowerLimit
End Function[/CODE]
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...
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...[CODE=vb]Option Explicit
Private Sub Form_Load()
End Sub[/CODE]
Select the entire window (just press Ctrl-A) then paste this code to replace it...[CODE=vb]Option Explicit
DefLng A-Z
Private Sub Form_Load()
' At startup, "seed" VB's pseudo-random number generator.
Randomize
End Sub
Private Sub cmdRandom_Click ()
' Each time the button is clicked, display
' a message box showing a random number between 1 and 10.
MsgBox "The random number generated is: " & Format(RandomNu mBetween(1, 10))
End Sub
Private Function RandomNumBetwee n(ByVal LowerLimit As Long, ByVal UpperLimit As Long) As Long
' This function returns a pseudo-random number between
' the specified limits (inclusive).
RandomNumBetwee n = Rnd * (UpperLimit - LowerLimit) + LowerLimit
End Function[/CODE]
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.
Comment