Ran dom Num bers
Random numbers are a pow erful tool. Of ten you know ex actly what you
want in your presentation and in ex actly what or der.
At other times you want to
mix things up randomly. For ex ample, you might want to practice ad dition facts,
but you don’t want to specify ev ery possible combination of one-digit numbers.
Instead you want the computer to ran domly generate prob lems for you. In an -
other example, you might have a large pool of ques tions, but you only want to
ask a few that are randomly se lected. This section explores these examples.
To have the computer
generate ran dom numbers, you need to know three
things:
Ran
dom
ize
,
Rnd
, and
Int
. For you math pur ists, computers can not gen -
erate truly
random numbers, but they can come close enough for al most any
purpose.
To be sure they are close enough for our pur poses, we need to make sure
that they are not the same every time.
That is why we start with a
Ran
dom
ize
statement. Just put this somewhere where it will be run before you need any ran -
dom numbers (such as in your
Ini
tial
ize
pro cedure). Imagine that the com-
puter has a big deck of cards with num bers on them.
When you ask for a ran dom
number, it picks the first card off the top of the deck and gives you the num ber on
it. When you ask for an other random number, it picks the next card. This deck of
cards starts out
in the same or der every time, so ev ery time you start the presenta-
tion and ask for a bunch of cards, you will get the same cards. This isn’t very
good. What we need is to shuffle the cards.
Do'stlaringiz bilan baham: