## Random Numbers

One of my favorite beginning programming concepts is random numbers. Random numbers allow you to build a wide variety of applications ranging from encryption to games. In this section, I show you how to build and use random numbers in your programs using two VBA functions called Randomize and Rnd.

The Randomize function initializes VBA's internal random number generator. It is only necessary to call this function (no argument required) once during the lifetime of your program. Most often, the Randomize function is executed during startup routines such as a form Load event. Without the Randomize function, the Rnd function generates random numbers in a consistent pattern, which of course is not really random at all.

The Rnd function takes a number as an argument and returns a Single data type. When used in conjunction with the Randomize function, the Rnd function can generate random numbers. To create a range of Integer-based random numbers, use the following VBA statements:

The Int function takes a number as argument and converts it to an integer value (whole number). Remember that the Rnd function returns a Single number type, so the Int function is used to convert a decimal number into a whole number. Adding 1 to the result of (10 * Rnd) creates a random number between 1 and 10. Removing the addition of 1 causes the random number range to be 0 through 9.

One way of utilizing random numbers is through a simulated dice roll. Figure 4.6 reveals the design-time form I used to simulate this. Note that there are eight images, six of which have their Visible property set to False. This way only two dice are visible to the user during runtime.

qFigure 4-6)

Using random numbers and image-swapping techniques to emulate rolling of the dice.

qFigure 4-6)

Using random numbers and image-swapping techniques to emulate rolling of the dice.

Randomize End Sub

Private Sub cmdRoll_Click()

Dim iRandomNumber As Integer

' Generate random number (die) for die 1. iRandomNumber = Int((6 * Rnd) + 1)

Select Case iRandomNumber

Case 1

imgDiel.Picture Case 2

imgDiel.Picture Case 3

imgDiel.Picture Case 4

imgDiel.Picture Case 5

imgDiel.Picture Case 6

imgDiel.Picture

End Select

' Generate random number (die) for die 2. iRandomNumber = Int((6 * Rnd) + l)

Select Case iRandomNumber

Case l imgDie2.Picture = Imagel.Picture Case 2

imgDie2.Picture = Image2.Picture

= Imagel.Picture

= Image2.Picture

= Image3.Picture

= Image4.Picture

= Image5.Picture

= Image6.Picture

Case 3

imgDie2.Picture = Image3.Picture Case 4

imgDie2.Picture = Image4.Picture Case 5

imgDie2.Picture = Image5.Picture Case 6

imgDie2.Picture = Image6.Picture End Select End Sub

To simulate the dice roll, I use the Click event of a command button to create a random number ranging from 1 to 6 for each die. After that, I perform a bit of image swapping based on a Select Case structure. Image swapping in this case is performed by assigning one Picture property to another. Remember, to work with programs that use images on the accompanying CD-ROM, you will need to change the path of the image's Picture property to a path on you local computer.

0 0