Random Numbers And Other Functions Random Numbers And Other Functions - Commodore SX-65 Portable User Manual

Portable color computer
Table of Contents

Advertisement

RANDOM NUMBERS AND OTHER FUNCTIONS
The 64 also has built-in functions that you can use to perform special tasks.
Functions are like built-in programs included in BASIC. The great advantage of
these built-in functions is that you don't have to type in a number of statements
every time you want to perform a specialized calculation. Instead, all you do is
type the command for ihe function you want and the computer does all the rest.
These built-in functions include figuring square roots (SQR), finding out the
contents of a memory location (PEEK), generating random numbers (RND), etc.
Appendix C lists all the functions available on your computer.
One function you can have a lot of fun with is the random number function,
RND. If you want to design a game or an educational program, you'll often need
to be able to program your computer to make up random numbers. For exam
ple, you'd need to do this to simulate the tossing of dice. Of course you could
write a program that wouid generate these random numbers, but it's much
easier to be able to do this just by calling upon the prewritten RaNDom number
function.
To see how RND works, try this short program:
NEW
10 FOR X = 1 TO 10
20 PRINT RND(1),
30 NEXT
IF YOU LEAVE OUT THE COMMA, YOUR
LIST OF NUMBERS APPEARS AS 1 COLUMN
When you RUN this program, the screen displays:
.789280697
.256373663
.682952381
.402342724
.158209063
.664673958
.0123442287
3.90587279E-04
.879300926
.245596701
Your numbers don't match? It would be incredible if they did because the
program generates a completely random list of ten numbers.
If you RUN the program a few more time, you'll see that the results are
always different. Though the numbers don't have a pattern, you'll notice a few
consistencies about the list the program displays.
For one thing, the results are always between 1 and 0. but never equal to 1 or
0. For another, the numbers are real numbers (with decimal points).
Now, we started out to simulate dice tosses, and the results from this pro
gram aren't exactly what we're looking for. Now we'll add a few more features
to this program to get what we want.
First, add this line to the program to replace line 20, and RUN the program
aaain:
20 PRINT 6*RND(1),
RUN
3.60563664
5.48602315
3.10045018
3.91302584
2.32056144
4.52687513
1.09650123
4.39052168
5.06321506
4.10781302
53
v
v
v
RANDOM NUMBERS AND OTHER FUNCTIONS
The
64 also
has buill·in
functions
that
you can
use
to
perform
special
tasks.
Functions
are
like built·in
programs included
in
BASIC.
The great advantage of
these built·in
functions is that you
don't have to type
in
a number
of statements
every time
you want
to perform a specialized calculation.
Instead,
all
you
do is
type the command for Ihe
funct
ion
you
want and the computer
does
all the
rest.
These
built·in
funct
ions
include figuring square roots (SOR), finding out the
contents of
a memory
location (PEEK),
generating random numbers
(RND),
etc.
Appendix C
lists
all the functions available
on you
r
computer.
One function you can have a lot
of
fun with
is
the
rand
om
number
function
,
RND.
If
you want
to design
a
game
or
an educational program,
yo
u'll
often need
to
be
able
to
program your
computer to
make
up
random
numbers. For
exam·
pie,
yo
u'd need to do this
to
simulate
the tossing
of dice. Of course you
could
write a
program that
wou
ld
generate these
random
numbers,
bu
t
it's
much
easier
to be
able to
do
this just
by
cal ling
upon the
prewritten RaNDom
number
function.
To see how
RND
works,
try
this
short program:
NEW
10 FOR
X
=
1
TO
10
20
PRINT RND
(1),
30
NEXT
IF
YOU LEAVE OUT THE COMMA.
YOUR
LIST
OF
NUMBERS
APPEARS
AS
1
COLUMN
When
you
RUN
this
program,
the screen
displays:
.789280697
.256373663
.682952381
.402342724
.158209063
.664673958
.0123442287
3.90587279E
- 04
.879300926
.245596701
Your numbers
don't
match? It
woul
d
be
incredible if
they
did
because the
program generates a completely random list
of
ten numbers.
If
you
RUN
the
program
a few more time, you'll
see
that the
results are
always different.
Th
ough the numbers
don
't
have a pattern, you'
ll
notice
a
few
cons istencies about
the
list the
program displays.
For
one thing
,
the results
are always between
1 and
0,
but never equal to
1
or
O.
For
another,
the numbers
are real
numbers (with decimal pOints).
Now,
we
started
out
to
simulate
dice
tosses, and the
results
from
this
pro.
gram aren't exactly what we're looking
for.
Now
we'll
add a few
more features
to
this program to get what we want.
First,
add this line to the program to replace line
20,
and
RUN
the program
again:
20 PRINT 6'RND(1),
RUN
3.60563664
5.48602315
3. 10045018
3.91302584
2.32056144
4.52687513
1.09650123
4.39052168
5.0632 1506
4.1078 1302
53

Hide quick links:

Advertisement

Table of Contents
loading

This manual is also suitable for:

Sx-64

Table of Contents