fortypoundhead.com

Making Random Numbers in VB6

Posted On 2009-10-31 by dwirch
Keywords: How to make truly random numbers with visual basic not psuedo random
Tags: VB6 Miscellaneous Tutorial Tip Windows
Views: 8456


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 the Author

dwirch has posted a total of 181 articles.

You can find more information from dwirch by visiting http://www.derekwirch.com.


Comments On This Post

By: VB6Boy
Date: 2009-10-31

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


Do you have a thought relating to this post? You can post your comment here. If you have an unrelated question, you can use the Q&A section to ask it.

Or you can drop a note to the administrators if you're not sure where you should post.


Your IP address is:54.158.238.108

Before you can post, you need to prove you are human. If you log in, this test goes away.




Recent Forum Posts

Advanced search added
dwirch posted on September 23, 2017 at about 13:44 in Site News

Job Spammer: Gaurav Mehta - AgreeYa Solutions
dwirch posted on September 22, 2017 at about 10:35 in Spammers

Job Spammer: Prutha Siri - Javelin Systems
dwirch posted on September 10, 2017 at about 6:15 in Spammers

New security implemented
dwirch posted on September 7, 2017 at about 7:16 in Site News

Malicious IP Checker Companion Tool
dwirch posted on August 12, 2017 at about 20:24 in Site News

Job Spammer: Steve Adams
dwirch posted on August 8, 2017 at about 7:44 in Spammers