Random Numbers

advertisement
Activity 12 - Random Numbers (Do it yourself – Assignment)
In this lesson, you will learn how to include random number generation inside your program.
Random numbers generation will make the computer seem intelligent or creative. Imagine
a math tutor that can randomly generate different questions and check your work! Let’s see
random numbers in action.
That’s Random
Create a form with a command button called “cmdRandomize” with caption = “Randomize”
Create a label on the form called lblDisplay. Each item will need the following code:
Option Explicit
Dim Num1 As Integer
Private Sub Form_Load()
Randomize
End Sub
Private Sub cmdRandomize_Click()
Num1 = Int(9 * Rnd) + 1
lblDisplay = Num1
End Sub
How it works:
 Create a variable called “Num1” as an integer. Integers are whole numbers.
 When the form loads, the Randomize command will initialize the random number
generator.
 Each time the Randomize button is clicked, a new random number is stored in
Num1. The numbers are in the range 1 to 9 inclusive (ie. Including 1 and 9).
How does this work: Int(9 * Rnd)+1
 The Rnd keyword is where the random number is placed each time it is generated.
However the random number is between {0  0.9999999}
 In order to expand the results to the range of 1 to 9, you must multiply the Rnd result
by 9. This expands the range to {0  8.9999999}
 The Int( ) takes the number from the previous step and truncates (ie. Chops off) the
decimals. So the number is now in the range {0  8}.
 Add one to the number so that it is now in the range {19}
Test yourself:
1) What is the statement to generate a random number between 1 and 6?
2) What is the statement to generate a random number between 5 and 10?
3) What is the statement to generate a random binary number (ie. either a 0 or 1 is
generated)?
Answers:
1) Int( 6 * Rnd ) + 1
2) Int( 6 * Rnd ) + 5
3) Int( 2 * Rnd )
The formula is:
Int( [High-Low +1] * Rnd) + Low
Where High is the upper limit of the range and Low is the lower limit of the range
If the range is from {1  High } where High is some number greater than 1, then the
formula reduces to Int( High * Rnd ) + 1
The Guessing Game revisited
Modify the guessing game to have the computer generate the random numbers.
Here is the program with the changes.
Option Explicit
Dim intSecretNum As Integer
Dim intGuess As Integer
Const intLowLimit = 5
Const intHighLimit = 50
Private Sub Form_Load()
Randomize
lblInstruction.Caption = "Guess a number between 5 and 50 inclusive.”
lblInstruction.FontSize = 14
lblInstruction.Font = "Arial"
cmdDone.Enabled = False
intSecretNum = Int((intHighLimit –intLowLimit + 1) * Rnd) + intLowLimit
End Sub
Private Sub cmdDone_Click()
Unload Me
End Sub
Private Sub cmdGuess_Click()
intGuess = Val(InputBox("What is your Guess?", "Guess me!"))
If (intGuess > intSecretNum) Then
MsgBox ("Your guess is too high")
End If
If (intGuess < intSecretNum) Then
MsgBox ("Your guess is too low")
End If
If (intGuess = intSecretNum) Then
MsgBox ("Your guess is correct!!!")
cmdDone.Enabled = True
cmdGuess.Enabled = False
End If
End Sub
Challenges
1) Create a program that will act as a math tutor and check if the user can add
numbers. When the user clicks on the Generate Question button, the program
randomly generates numbers to be added and fills the textboxes with these two
numbers. The numbers should be between 1 and 10. The user can enter their
answer into the last text box. When the user clicks on the Answer button, the
program displays a message box indicating whether the answer was correct or
incorrect. Each time that the user clicks on the Generate Question button, a new
set of numbers is generated. You will need to declare variables to store the user
correct answer as well as the user’s answer in order for the program to compare
results.
2) Modify the first example “That’s Random” to create a dice program. The program
rolls two die. It must generate two random numbers between 1 and 6 and add them.
The user is then informed if they have rolled “Snake Eyes” (sum = 2), “Lucky Seven”
(sum = 7), or “Roll Again for Free” (12).
3) The ultimate math tutor. Modify the first challenge to also randomly generate the
operation (either +, -, *, or \). You will need to randomly generate numbers 1 to 4 to
represent one of the four operations. Then calculate what the correct answer should
be based on the random operation that was generated. Don’t forget to update the
label on the form with the random operation.
Download