Generating Random Numbers in VB6

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Killer42
    Recognized Expert Expert
    • Oct 2006
    • 8429

    Generating Random Numbers in VB6

    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...
    • 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
  • Hitronic
    New Member
    • Sep 2023
    • 1

    #2
    Problem in Main Function

    There is an significat Problem in the main Function RandomNumBetwee n :
    If you use the automatic VB Conversion from Double/Single to Int VB uses the Round Function
    and this leads to a mathematically uneven distribution.
    The smallest and largest values occur only half as often.

    Here is a (hopefully) correct version :

    Code:
    Private Function RandomNumBetween(ByVal LowerLimit As Long, ByVal UpperLimit As Long) As Long
       ' This function returns a pseudo-random number between
       ' the specified limits (inclusive).
       RandomNumBetween = Int (Rnd * (UpperLimit + 1 - LowerLimit)) + LowerLimit
    End Function

    Comment

    • Tomas Fok
      Banned
      New Member
      • Nov 2023
      • 1

      #3
      Nowadays, ensuring the security of one's own home is becoming a priority for many. With the increasing threat of intrusion and crime, professional CCTV camera installation services are becoming a key part of the security system, ensuring that your home is always monitored and protected.
      Expert network cabling installation in Toronto & GTA. High-speed, structured, and fiber optic cabling for homes & businesses with a 3-year warranty.

      Professional services provide access to advanced technology in the field of video surveillance. These include cameras with various functionalities : indoor, outdoor, night vision, high resolution, etc. Specialists recommend and install the equipment according to the specific needs of the client.

      The installation stage plays a crucial role in ensuring the reliability of the system. The experts not only place the cameras in strategic locations, but also configure them for maximum efficiency. This includes optimizing viewing angles, motion sensitivity, and other parameters that allow the system to respond to potential threats.

      Comment

      Working...