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.
Name the top label lblTitle.
Name the text box txtGuess.
Name the command button on the left cmdGuess.
Set the Enabled property of cmdGuess to False.
Name the command button on the right cmdShow.
Set the captions of the command buttons as seen above.
To place the option buttons in the frame, place the buttons first.
Set the captions of the option buttons as seen above.
Name the option buttons as follows from left to right:
optRange1
optRange2
optRange3
Set the Value property of the option button optRange1 to True to select this option button.
Size the option buttons to fit around their captions.
Place a frame on the form.
Set the caption of the frame as seen above.
Size the frame to 4455 in Width and 735 in Height.
Cut the option buttons from the form by pressing Ctrl then X.
Select the frame, then press Ctrl V.
Position them appropriately if necessary.
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.
Open the code window for the option button optRange1.
Press tab, then type the following lines:
bytRange = 0
intTries = 0
Call Random
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.
Select the optRange2 option button in the code window's object box.
Press tab, then type the following lines:
bytRange = 1
intTries = 0
Call Random
Select the optRange3 option button in the code window's object box.
Press tab, then type the following lines:
bytRange = 2
intTries = 0
Call Random
Select the txtGuess object in the object box of the code window.
Press tab, then type If Len(txtGuess.Text) > 0 Then cmdGuess.Enabled = True Else cmdGuess.Enabled = False.
This line disables the command button cmdGuess when there is no text in the text box txtGuess.
Select the Form object in the object box of the code window.
Press tab, then type the following lines:
bytRange = 0
intTries = 0
intDimension = 0
Call Random
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.
Select General in the object box of the code window, then type the following lines:
Option Explicit
Dim intDimension As Integer, bytPlace As Byte
Dim bolCheat As Boolean, strNumbers(100) As String
Dim lngNumber As Long, intTries As Integer, bytRange As Byte
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.
Press enter twice after the last line you typed in the code windows General section.
Type the following lines:
Private Function Random()
Select Case bytRange
Case 0
Randomize
lngNumber = Int(Rnd * 1000)
Case 1
Randomize
lngNumber = Int(Rnd * 10000)
Case 2
Randomize
lngNumber =
Int(Rnd * 100000)
End Select
End Function
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.
Open the code window for the cmdShow command button.
Press tab, then type the following lines:
bolCheat = True
frmRandom.Caption = lngNumber
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.
Select the cmdGuess object in the object box of the code window.
Press tab, then type the following lines:
Dim lngGuess As Long
intTries = intTries + 1
lngGuess = Val(txtGuess.Text)
frmRandom.Caption = ""
Select Case lngGuess
Case Is < lngNumber
MsgBox "Guess higher.", vbExclamation, ""
Case Is > lngNumber
MsgBox "Guess lower.", vbExclamation, ""
Case Is = lngNumber
If bolCheat = True Then
MsgBox "Haha! You had to cheat!!", vbExclamation, ""
bolCheat = False
intTries = 0
strNumbers(intDimension) = lngNumber
intDimension = intDimension + 1
Random
Exit Sub
End If
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.
Below the last line you typed, enter the following lines:
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.