EasyManua.ls Logo

Commodore 128D - Generating Random Numbers-The RND Function

Commodore 128D
427 pages
Print Icon
To Next Page IconTo Next Page
To Next Page IconTo Next Page
To Previous Page IconTo Previous Page
To Previous Page IconTo Previous Page
Loading...
u
The
computer
added
0.5
to
4.75
before
it
executed
the
INT
function,
so
that
it
rounded
5.25
down
to
5
for
the
result.
If
you
want
to
round
~]
off
the
result
of
a
calculation,
do
this:
^—»
?
lNT((100/6)
+
0.S)
u""ur
You
can
substitute
any
calculation
for
the
division
shown
in
the
inner
'—'
parentheses.
What
it
you
want
to
round
off
numbers
to
the
nearest
0.01
?
Instead
of
!—'
adding
0.5
to
your
number,
add
0.005,
then
multiply
by
100.
Let's
say
you
want
to
round
2.876
to
the
nearest
0.01.
Using
this
method,
you
r~j
start
with:
I—I
?(2.876
+
0.005)*100
)\f
\\f\\W
Now
use
the
INT
function
to
get
rid
of
everything
after
the
decimal
{—'
point
(which
moves
two
places
to
the
right
when
you
multiply
by
100).
You
are
left
with:
p~l
?
INT«2.876
+
0.005)*100)
III
I
^
which
gives
you
a
value
of
288.
All
that's
left
to
do
is
divide
by
100
to
j
,
get
the
value
of
2.88,
which
is
the
answer
you
want.
Using
this
tech-
'—'
nique,
you
can
round
off
calculations
like
the
following
to
the
nearest
0.01:
,-,
?
INT((2.876
+1.29
+16.1
-
9.534
+
0.005)*
100)/100
Generating
Random
Numbers—The
RND
Function
The
RND
functions
tells
the
computer
to
generate
a
random
number.
This
can
be
useful
in
simulating
games
of
chance,
and
in
creating
interesting
graphic
or
music
programs.
All
random
(RND)
numbers
are
nine
digits,
in
decimal
form,
between
the
values
0.000000001
and
0.999999999.
Type:
Multiplying
the
randomly
generated
number
by
six
makes
the
range
of
generated
numbers
increase
to
greater
than
0
and
less
than
6.
In
order
to
include
6
among
the
numbers
generated,
we
add
one
to
the
result
of
RND(0)*6.
This
makes
the
range
1
<X<7.
If
we
use
the
INT
function
to
eliminate
the
decimal
places,
the
command
will
generate
whole
numbers
from
1
to
6.
This
process
can be
used
to
simulate
the
rolling
of
a
die.
Try
this
program:
10
R=
INT(RND(1)*6
+
1)
20
?R
30
GOTO
10
68
USING
C128
MODE—Advanced
BASIC
Programming

Table of Contents

Other manuals for Commodore 128D

Related product manuals