Search Tools Links Login

Making Random Numbers in VB6


Some time ago, a budding young VB programmer was having a problem with random numbers not being random, but a predictable repeating pattern. It can be easily overcome, and here is how.

If you want to just see the code, scroll to the bottom of this article, and all shall be revealed. If you want to read the why of random numbers not being random, read on.

Most computers don't generate truly random values. To be truly random, the value must not be predictable. Computers are not in the business of being random. Computers use complex mathematical formulas to generate values which look random, but really aren't. Even though the formulas are complex, they're still predictable.

Each time a program is run, the psuedo-random values returned by Rnd are always the same. Obviously, this is very predictable and not random at all. However, VB has also provided the Randomize method.

The problem with Randomize is that unless you specify a seed, you are left with the same problem; the same string of psuedo random numbers are being picked. So why not specify a seed thus:

Randomize 12345

Ok, you've specified a seed. And it works ... almost. You've gotten a new string of random numbers, but now you've hard coded the seed into your program, so all your random number choosings will be same again, based on that seed.

Not cool. You could work around it by asking the user for some number. Some encryption systems are based on this, and this method was also widely used in the early days of BASIC.

So why not seed the generator with a more hard to pin down seed? But where, oh where, are we going to find an easily accessible, system supplied, visible-to-VB, constantly changing number?

Something like the system timer.

In computer science and computer programming, system time represents a computer system's notion of the passing of time. In this sense, time also includes the passing of days on the calendar.

System time is measured by a system clock, which is typically implemented as a simple count of the number of ticks that have transpired since some arbitrary starting date, called the epoch. For example, Unix and POSIX-compliant systems encode system time as the number of seconds elapsed since the start of the epoch at 1 January 1970 00:00:00 UT. Windows NT counts the number of 100-nanosecond ticks since 1 January 1601 00:00:00 UT as reckoned in the proleptic Gregorian calendar, but returns the current time to the nearest millisecond.

By using the current value of the system timer, you can almost be gauranteed that you will get a random string of numbers every time. I say almost because, technically, it is possible to stumble across the same millisecond when pulling the system time. But how likely is it you can press a key with 1/1000th of a second accuracy? Not very likely.

So without further adieu, here is the VB6 code that will generate 10 random numbers between 1 and 13. The minimum and maximum values are stored in "RandomMin" and "RandomMax".

Take a look, and use it freely. Simple code, really, but this can be helpful someone out there, I'm sure.

Dim MyRandomNumber As Long 'The chosen number
Dim RandomMax As Long 'top end of range to pick from
Dim RandomMin As Long 'low end of range to pick from
Dim Kount As Long 'loop to pick ten random numbers

RandomMin = 1
RandomMax = 13

Randomize Timer

For Kount = 1 To 10

   MyRandomNumber = Int(Rnd(1) * RandomMax) + RandomMin
   Debug.Print MyRandomNumber

Next

About this post

Posted: 2009-10-31
By: dwirch
Viewed: 11,399 times

Categories

Tip

Tutorials

Windows

Visual Basic 6

Attachments

No attachments for this post


Loading Comments ...

Comments

vb6boy posted this comment on 2009-10-31:

Thanks! This has been kicking my butt for quite awhile, and this is exactly what I was looking for.

AnonymousCoward posted this comment on 2018-02-21:

That code helped me figured out that I needed to use randomize timer in my own code. Thanks

You must be logged in to make a comment.