lesson 2.4 random numbers and built-in functions
TRANSCRIPT
-
8/19/2019 Lesson 2.4 Random Numbers and Built-In Functions
1/38
-
8/19/2019 Lesson 2.4 Random Numbers and Built-In Functions
2/38
Lesson 2.4
Random Numbersand Built-InFunctions
-
8/19/2019 Lesson 2.4 Random Numbers and Built-In Functions
3/38
Objectives
!t t"e end o# t"e lesson$ %ou s"ouldbe able to
&. describe t"e utilit% o# some o#
'B(s built-in #unctions2. evaluate e)*ressions t"at use
t"ese #unctions+ and
,. enerate random numbers.
-
8/19/2019 Lesson 2.4 Random Numbers and Built-In Functions
4/38
/"e eneration o# random
numbers is too im*ortant to bele#t to c"ance0. 1R. Cove%ou
-One o# t"e assets o# 'B is its abilit%
to enerate random numbers.Randomness is im*ortant in
com*uter amin as %ou ma% "ave
e)*erienced. /"ere is not"in
orse t"an a *redictable ame !I.O# course$ randomness needs to be
controlled.
-
8/19/2019 Lesson 2.4 Random Numbers and Built-In Functions
5/38
! 3uessin 3ame
Let us construct a ame "ereint"e user tries to uess a number
"eld b% t"e com*uter. /o "el* t"e
user$ t"e com*uter s"ould *rom*t
t"e user to mae a loer or a"i"er uess i# "is5"er uess is
incorrect. 6e s"all need a #orm
it" a te)t bo) namedtxtGuess
and a command button named
cmdOk$ "ose code ill loo
somet"in lie t"is
-
8/19/2019 Lesson 2.4 Random Numbers and Built-In Functions
6/38
7im 3uess !s Inteer
7im Remar !s Strin
Const Number !s Inteer 8 ,9
:rivate Sub cmdO;Clic
-
8/19/2019 Lesson 2.4 Random Numbers and Built-In Functions
7/38
?lseI# 3uess @ Number /"en
Remar 8 AI3A?R0
?lse
Remar 8 RI3A/0
?nd I#
sBo) Remar$ 4$ Simon Sa%s0
?nd Sub
-
8/19/2019 Lesson 2.4 Random Numbers and Built-In Functions
8/38
! 3uessin 3ame
Dou "ave *robabl% noticedsomet"in else-t"e sBo)
statement. /"e sBo)
statement *roduces a *o*-u*
messae bo) t"at *rom*ts t"e
user to clic on a command
button
-
8/19/2019 Lesson 2.4 Random Numbers and Built-In Functions
9/38
!natom% o# a essae Bo)
sBo) message, style, title
/"e messae is t"at "ic" a**ears
in t"e messae bo)
-
8/19/2019 Lesson 2.4 Random Numbers and Built-In Functions
10/38
For t"e buttons
VALU E
NAME
D
CONS
TANT
BUTT ONS
DISPL
AYED
0 vbOk Only
Okbutton
1vbOk Canc el
Ok
andCancelbutton
continuationE
!natom% o# a essae Bo)
-
8/19/2019 Lesson 2.4 Random Numbers and Built-In Functions
11/38
For t"e icon
continuationE
!natom% o# a essae Bo)
Val ue
Name
d
Const
ant
Icon
1) vbC"*t *cal
&! vb+u
et*on
(, vbE-c la.at*
on
)( vbIn/
-
8/19/2019 Lesson 2.4 Random Numbers and Built-In Functions
12/38
For e)am*le$ %ou ant Des andNo buttons to a**ear toet"er
it" t"e critical icon$ let st%le be
t"e sum o# t"e values
-
8/19/2019 Lesson 2.4 Random Numbers and Built-In Functions
13/38
Note t"at %ou can include in %ourcode actions t"at de*end on t"e
command button cliced in t"e
messae bo)$ but t"at is u* to %ou to
e)*lore.
continuationE
!natom% o# a essae Bo)
-
8/19/2019 Lesson 2.4 Random Numbers and Built-In Functions
14/38
Construct t"e uessin ame *roram
e(ve just discussed. 'eri#% i# it ors.
Try this on your computer:
continuationE
!natom% o# a essae Bo)
-
8/19/2019 Lesson 2.4 Random Numbers and Built-In Functions
15/38
3eneratin a Random Number
Dou ma% "ave noticed to t"ins entron it" our *roram. One is t"at as
user can onl% *la% t"is ame once
-
8/19/2019 Lesson 2.4 Random Numbers and Built-In Functions
16/38
3eneratin a Random Number
Fortunatel%$ 'B "as somet"in t"at can"el* a *seudo variable called Rnd. Rnd is
a variable in t"e sense t"at it re*resents
a value$ but unlie a real variable$ t"at
value c"anes eac" time %ou re#er to it.
continuationE
-
8/19/2019 Lesson 2.4 Random Numbers and Built-In Functions
17/38
Construct a #orm it" a te)t bo)
named t)tOut*ut and a command
button called cmd3enerate. /o t"ecommand button$ assin t"e
#olloin code.
Sub cmd3enerate;Clic< =
t)tOut*ut./e)t 8 Rnd
?nd Sub
Try this on your computer:
continuationE
3eneratin a Random Number
-
8/19/2019 Lesson 2.4 Random Numbers and Built-In Functions
18/38
i i
-
8/19/2019 Lesson 2.4 Random Numbers and Built-In Functions
19/38
6e can no enerate numbers beteen G and&G but "at e reall% need #or t"e uessin
ame *roram are "ole numbers. In comes a
built-in #unction called Fi). Fi) dro*s t"e
decimal *ortion o# a number suc" t"atFi)
-
8/19/2019 Lesson 2.4 Random Numbers and Built-In Functions
20/38
Note t"at e can no enerate randominteers #rom G to 9. 6"at i# e ant
numbers #rom & to &GJ /"e solution is
sim*le.
Sub cmd3enerate;Clic
-
8/19/2019 Lesson 2.4 Random Numbers and Built-In Functions
21/38
Note t"at$ in eneral$ Fi)
-
8/19/2019 Lesson 2.4 Random Numbers and Built-In Functions
22/38
Un#ortunatel%$ it seems t"at our *roramenerates t"e same set o# random
numbers. /"e reason is somet"in called
a random number seed or random seed.
Imaine our com*uter standin at t"eede o# a *ool o# random numbers. I# t"e
com*uter ere to stand in t"e same
s*ot$ it ould be ettin t"e same set o#numbers. /"e random seed dictates
"ere around t"e *ool t"e com*uter
s"ould stand.
continuationE
3eneratin a Random Number
ti ti
-
8/19/2019 Lesson 2.4 Random Numbers and Built-In Functions
23/38
/"e *roblem is t"at e need t"atrandom seed to be di##erent ever% time
e run t"e *roram. So$ "at c"anes
ever% time e run t"e *roramJ /ime
continuationE
3eneratin a Random Number
ti ti
-
8/19/2019 Lesson 2.4 Random Numbers and Built-In Functions
24/38
6e need to et t"e random seed #romt"e internal cloc o# t"e com*uter. /"e
statement #or t"at is as #ollos
continuationE
3eneratin a Random Number
Sub cmd3enerate;Clic< =
RandomiMe /imer
t)tOut*ut./e)t 8 Fi)
-
8/19/2019 Lesson 2.4 Random Numbers and Built-In Functions
25/38
6e are no read% to mae t"e uessiname *roram more #un. Su**ose e
ant t"e number to be uessed to be
#rom & to &GG. O# course t"is reuires
t"e e)*ression Fi)
-
8/19/2019 Lesson 2.4 Random Numbers and Built-In Functions
26/38
:lacin it it"in t"e cmdO;clic
-
8/19/2019 Lesson 2.4 Random Numbers and Built-In Functions
27/38
:rivate Sub Form ;
-
8/19/2019 Lesson 2.4 Random Numbers and Built-In Functions
28/38
-
8/19/2019 Lesson 2.4 Random Numbers and Built-In Functions
29/38
So #ar e(ve been introduced to too# 'B(s built 1in #unctions. /"ese
are 'al and Fi). Recall t"at 'al
-
8/19/2019 Lesson 2.4 Random Numbers and Built-In Functions
30/38
Ot"er Built-in Functionsunct*o
n
Dec"*t*on
Ab2N3
4et t5eabolutevalue o/ N
E-' Ab2&3 6 Ab27&3 6 &
Int2N3
#ound do8nN to t5enea"et*nte$e" 96N'E-' Int21'!3 6Int213 6Int21':3 6 1
Int271')3 6 Int27 1'1 6 Int 7! 6
continuationE
continuation
-
8/19/2019 Lesson 2.4 Random Numbers and Built-In Functions
31/38
Remember t"at t"ese #unctions canbe used in conjunction it"
arit"metic o*erators. For e)am*le$
consider t"e uadratic #ormula t"at#ollos
Ot"er Built-in Functions
continuationE
continuation
-
8/19/2019 Lesson 2.4 Random Numbers and Built-In Functions
32/38
O# course$ e do not "ave ano*erator lie so in code e need
to lines
Ot"er Built-in Functions
continuationE
)& 8
-
8/19/2019 Lesson 2.4 Random Numbers and Built-In Functions
33/38
/"ese are called numeric #unctionsbecause t"e% result in numeric
values. 'B also o##ers us several
#unctions t"at result in strinvalues.
Ot"er Built-in Functions
continuationE
-
8/19/2019 Lesson 2.4 Random Numbers and Built-In Functions
34/38
Some Built-in Strin Functionsu nct
*on
Dec"*t*on
LC ae
2St "*n$3
Conve"t t5et"*n$>c5a"acte" to
lo8e"cae'E-'LCae2?Bob@36 ?bob@
LCae2?BOB@36 ?bob@ Lcae2?bob@36 ?bob@
UC Conve"t t5e
continuation
-
8/19/2019 Lesson 2.4 Random Numbers and Built-In Functions
35/38
Lcase and Ucase are *articularl%
use#ul in *rorams reuirin te)tin*ut. For e)am*le$ consider t"e
Branc"es o# 3overnment *roram in
Lesson 2.2.
continuationE
Some Built-in Strin Functions
-
8/19/2019 Lesson 2.4 Random Numbers and Built-In Functions
36/38
continuationE
l
-
8/19/2019 Lesson 2.4 Random Numbers and Built-In Functions
37/38
Notice t"at i# %ou enter PU7ICI!L or
Pudicial$ it ill #ail to dis*la% Su*remeCourt. /"e *roram is case-sensitive
/o mae it case-insensitive$ *rocess
t"e user(s in*ut to matc" t"e o*tion.In t"is case Lcase is t"e e%.
continuationE
Some Built-in Strin Functions
-
8/19/2019 Lesson 2.4 Random Numbers and Built-In Functions
38/38