Random

advertisement
ARITHMETIC EXPRESSIONS
Arithmetic expressions in VB look much like ordinary arithmetic expressions that you would see in a Math
course. The basic arithmetic operations are shown below:
Operation
addition
subtraction
multiplication
division
exponents
Operator
+
*
/
^
The normal order of operations still applies, with multiplication and division being done from left to right
before addition and subtraction. Parentheses, (but not square [] or curly {} brackets!) can be used to change the
order of evaluation.
In addition to the standard operators, VB includes two other division operators. The \ operator performs Integer
division, and the Mod operator performs modulus division. Integer division truncates the decimal portion of the
quotient which results in an integer,
Example:
Dim intX As Integer
IntX = 20\7
‘ intX is assigned 2
Modulus division returns the remainder resulting from the division
Example:
Dim intX As Integer
IntX = 20 Mod 7
‘ intX is assigned 6
Modulus division is often useful for examining the digits of a number, finding the number of minutes left over
after the hours have been accounted for or for determining if a number is even or odd.
In arithmetic expressions, integer division is performed after multiplication and division. Modulus division is
performed next, and then addition and subtraction. For example 5* 2 Mod 3\2 = 0, because 5*2 is done first,
then 3\2=1, then 10 mod 1 =0
RANDOM NUMBERS
Games, simulators, screen savers, and many other types of applications require random numbers. Computers
cannot make random choices, but we can make their choices appear random – pseudo-random. VB includes a
built-in Rnd function that generates a random number greater than or equal to 0 and less than 1. ( 0<=x<1 )
Lblrandom.text = Rnd
Lblrandom.text = Rnd * 10
Lblrandom.text = Rnd + 5
‘ Returns a random number between 0 and < 1
‘ Returns a random number between 0 and < 10
‘ Returns a random number between 5 and < 6
Random numbers can also be generated in a range using the following expression:
(Highnumber – Lownumber + 1) * Rnd + LowNumber
Where Highnumber represents the maximum value desired, and LowNumber represents the minimum value.
Example:
To get numbers between 10 and 30
((30-10 + 1) * Rnd + 10)
Lblrandom.text =21 * Rnd + 10
‘ Returns a random number between 10 and 30 inclusive
The Int Function returns the integer portion of a number without rounding.
Lblrandom.text = Int (21 * Rnd + 10)
‘ Returns a random integer between 10 and 30 inclusive
*******Note ***********
Programs using Rnd should also include a randomize statement. The Randomize statement initializes the VB
Rnd function so that different random numbers are generated in a program from run to run. The randomize
statement should be executed only once in a program and is therefore best placed in the Form_Load procedure
where it is called when the application is started:
Private Sub Form_Load()
Randomize
End Sub
‘ Initialize random number generator
PRACTICE EXERCISES – NOTE 3
1. Practice with nested if statements:
Write a program that simulates a
Magic Eight Ball. It will ask the
user for a question and then shake
up a response. You should
randomly choose between 8
possible answers. [3 marks]
2. Create a Math Program that will:
a. Randomly choose and
display two integers
between 1 and 15 in
labels.
b. Randomly choose and
display either an addition
sign or a subtraction sign in another label.
c. Provide a textbox to allow the user to enter the answer.
d. Include a click button that when clicked will determine if the answer is correct.
e. Use the Msgbox command to indicate if the answer is correct or not.
f. Keep track of the number of correct questions.
g. Include a Quit button that will end the program. Before it ends provide a goodbye message
telling them their percentage in a msgbox. [5 marks]
CHECK FOR KNOWLEDGE
1. Write the code necessary to pick a random integer between 1 and 50.
2. Write the code necessary to pick a random number between -10 and 10.
3. Write the code necessary to pick a random number between -67 and 14.
4. What is the outcome of: a) 18 mod 4 b) 18/4 c) 18\4 d) int(4 * rnd +2)
Download