Visual Basic Programming

Page 26

On the next page, we will use another array and access the different dimensions at random. To do so, we need to learn how to use random numbers. Visual Basic includes a function called Rnd, which generates a random decimal number between zero and one.

Now we can code this program. We are going to create a function called Random to generate the random number. But first, we will code the option buttons.

I am sure that you understand this code. Remember that Call simply tells the code to execute the code in the specified procedure or function, in this case, the Random function.

This line disables the command button cmdGuess when there is no text in the text box txtGuess.

The first three lines set the variables we will declare shortly equal to a value to set the initial values of them. The last line calls the function we will create shortly to generate the random number.

Line 1: Tell Visual Basic to cause an error when the code uses an undeclared variable.
Line 2: Declare variable intDimension as an integer, which will look in a dimension of an array we will use. Declare variable bytPlace as a byte, which will be used to look in the array as well.
Line 3: Declare variable bolCheat as a boolean, which will determine if the user decides to see the number ahead of time. Declare string variable strNumbers as a String array of one hundred dimensions, which will be used to prevent the use of a number through one hundred tries.
Line 4: Declare variable lngNumber as a long, which will be used to store the random number. Declare variable intTries as an integer, which will be used to determine how many tries it takes the user to guess the number. Declare variable bytRange as a byte, which will be used as a flag to determine the range of the random number.

Line 1 of this code begins a function, then line 2 tests the value of bytRange.
Line 4: When bytRange is 0 (tested in line 3), initialize the random number generator using the Randomize function. This should always be used when using random numbers.
Line 5: Set the value of the variable lngNumber to a random number (Rnd) times 1000, then convert that number to an integer, thus rounding it to a whole number.
Line 8: When bytRange is 1 (tested in line 6), set the variable lngNumber to a random number times 10000, then convert that number to an integer.
Line 11: When bytRange is 2 (tested in line 9), set the variable lngNumber to a random number times 100000, then convert that number to an integer.
Line 12 ends the Case selection, and line 13 ends the Function.

It is very simple to determine the range of a random number. When we say Int(Rnd * 1000), it generates numbers between 0 and 1000, and rounds to the nearest integer. The function Rnd always generates a number between 0 and 1. By multiplying the number by something, we generate numbers between 0 and whatever number we multiply the Rnd function by. Rnd * 20 generates decimal numbers between 0 and 20; Rnd * 500 generates decimal numbers between 0 and 500, and so forth. The Int function then takes the resulting random number and converts it to the nearest integer (non-decimal number).

Suppose the Rnd function returns the decimal number .23547348. The numbers are usually this crazy. The lines above multiply that number by 1000, thus equaling 235.46248. The Int function converts 235.46248 to an integer, which is by definition a non-decimal or whole number, including negative and positive. The function will round this value to the nearest integer, which is 235.

Line 1: Set the variable bolCheat to True, which acts as a flag later to see if the user displays the number before they guess it.
Line 2: Display lngNumber in the caption of the form.

Line 1: Declare variable lngGuess as a Long data-type to store the user's guess.
Line 2: Increment the variable intTries by one.
Line 3: Store the value of the text in the text box txtGuess in the variable lngGuess.
Line 4: Clear the caption of the form.
Line 5: Begin selection of the users guess.
Line 7: Display a message box telling the user to Guess higher when their guess (lngGuess) is less than lngNumber.
Line 9: Display a message box telling the user to Guess lower when their guess (lngGuess) is greater than lngNumber.
Line 10: Test to see if the user's guess equals the random number.
Line 11: Test to see if the user has displayed the number previously (bolCheat is set to True in the code window for the object cmdShow).
Line 12: Display a message box when the user displays the number.
Line 13: Reset variable bolCheat.
Line 14: Reset variable intTries.
Line 15: Set array element intDimension of strNumbers array to the generated random number (lngNumber).
Line 16: Increment the variable intDimension so that this variable writes to the next part of the array the next time through in the Line 15.
Line 17: Call the random number function Random to get a new number.
Line 18: Exit the sub-procedure so that the lines below it are not executed. We will enter more code in the next step.

    MsgBox "Correct! You guessed the number in " & intTries & " tries.", vbExclamation, ""
    strNumbers(intDimension) = lngNumber
    intDimension = intDimension + 1
    intTries = 0
    If intDimension < 101 Then
        For bytPlace = 0 To 100
            Do
                Random
            Loop Until Val(strNumbers(bytPlace)) <> lngNumber
        Next bytPlace
    Else
        For bytPlace = 0 To 100
            strNumbers(bytPlace) = ""
        Next bytPlace
        intDimension = 0
    End If
End Select
txtGuess.SetFocus

Line 1: Display a message box in the exclamation form to tell the user that they guessed the number in the amount of tries as intTries.
Line 2: Store the number that the user just guessed in the array strNumbers, using the value of the variable intDimension to specify the index number.
Line 3: Increment the variable intDimension by one. This allows the line above to store the number into the appropriate array dimension.
Line 4: Reset the variable intTries to zero.
Line 5: Test the value of intDimension (make sure it is less than 101 so that it will not look at a non-existing array dimension. Remember that the array does not go above 100).
Line 6: Look at values of bytPlace from 0 to 100.
Line 7: Begin Do Until loop.
Line 8: Call the Random function created earlier until the condition in line 9 is met.
Line 9: Call Random until the value of the number in the array element bytPlace in array strNumbers does not equal lngNumber (the generated random number).
Line 10: Look at the next value of bytPlace in order to search the array 0 to 100.
Line 11: Otherwise (the value of the variable intDimension is equal to 101, thus exceeding the dimensions of the array), do below.
Line 12: Look at the value of bytPlace from 0 to 100.
Line 13: Clear the entire array in order to start over. The array goes 0 to 100, just like the loop, thus clearing the array.
Line 15: Reset the variable intDimension.
Line 18: Set the focus (send the cursor) to the txtGuess text box.

This program should work just fine now. I hope you enjoyed this exercise in random numbers. Now we may proceed to Page 27.

Top of Page

Page 1 | Page 2 | Page 3 | Page 4 | Page 5 | Page 6 | Page 7 | Page 8 | Page 9 | Page 10 | Page 11

Page 12 | Page 13 | Page 14 | Page 15 | Page 16 | Page 17 | Page 18 | Page 19 | Page 20

Page 21 | Page 22 | Page 23 | Page 24 | Page 25 | Page 26 | Page 27 | Page 28 | Page 29 | Page 30