Introduction: Random Numbers

A random number is often used in games and graphic programs to make the game unpredictable.

To generate a random number in your code, you first need to determine the range of numbers you want to generate. For example, you may want to generate random numbers between 1 and 10, between 0 and 100, etc.

Once you know the numbers you want to generate, you use the following code: intRandom = Rnd() * x where x is what will determine the range of random numbers you want to generate.

The Rnd() on its own, without the multiplier, always includes the number 0 and generates a random decimal between 0 and 1. So, to get other numbers, you need to multiply and then add or subtract to get the proper range.

Observe the following Rnd calls and the numbers they generate:

intRandom = Rnd()*9 ' generates a random integer between 0 and 9

intRandom = Rnd()*10 ' generates a random integer between 0 and 10

intRandom = Rnd()*9 + 1 ' generates a random integer between 1 and 10

Also, you need to let the program know you want to start with different random numbers each time you run, so you must include the function Randomize() in your code before you call the Rnd function. This only needs to be called once each time the program runs, so the form load event procedure is a good place to call the Randomize function. Without the Randomize function call, the program will start with the same random number each time.