473,406 Members | 2,390 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes and contribute your articles to a community of 473,406 developers and data experts.

Generating Random Numbers in VB6

8,435 Expert 8TB
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, 2767 views)
May 4 '07 #1
2 85957
Hitronic
1 Bit
There is an significat Problem in the main Function RandomNumBetween :
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 :

Expand|Select|Wrap|Line Numbers
  1. Private Function RandomNumBetween(ByVal LowerLimit As Long, ByVal UpperLimit As Long) As Long
  2.    ' This function returns a pseudo-random number between
  3.    ' the specified limits (inclusive).
  4.    RandomNumBetween = Int (Rnd * (UpperLimit + 1 - LowerLimit)) + LowerLimit
  5. End Function
Sep 16 '23 #2
Tomas Fok
1 Bit
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.
https://sense-group.pro/network-cabling/
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.
Nov 21 '23 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Intaek LIM | last post by:
generally, we use srand(time(0)) to generate random numbers. i know why we use time(0), but i can not explain how it operates. first, see example source below. ...
2
by: Mike P | last post by:
I have a method that I am using to generate random numbers using Random.Next. However, every time I call the method I get the same random number. Is there a C# equivalent of Randomize is VB6? ...
10
by: Glenn Wilson | last post by:
I have a quick Question and I Hope some one can help or at least explain. What is happening is that I am trying to use random numbers in an application, as per the sample test code below. When I...
5
by: dav3 | last post by:
I have almost completed a monster assignment on sorting algorithms (quick, insertion and selection) using c++ but I am lost on one part of the assignment. I have to generate a random list of numbers...
1
by: Velhari | last post by:
Hi, I am a beginner. Please tell me, For generating Random Numbers, Initially why we are going for seed method. And another question is that, I want to print unique random number how to print by...
8
by: kiranchahar | last post by:
Hey all, How do I generate random numbers with Uniform distribution Uniform(a,b) using C-programming? I want to generate uniform random numbers which have mean following Uniform(p,q) and also...
0
SammyB
by: SammyB | last post by:
These are some "random" thoughts about generating random numbers in Visual Basic. Wikipedia will give a better introduction than I, see http://en.wikipedia.org/wiki/Random_number_generator. ...
2
by: enrique21 | last post by:
Im trying to generate random numbers using the statement: Random r = new Random(2); for (int i=1;i<=18;i++) but im having errors in my results here is my entire code so far: import...
12
by: 9966 | last post by:
Greetings, This is my second post till now. Thanks for all the advice given to me for the first post. Now I'm having problem with generating random numbers. I know if we want to generate a...
3
by: diggity | last post by:
okay, so i started on code for Sudoku on ready to program java ... very close to java. I was able to create a 2d array and display and change the variables. What i want to do now is to generate...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.