range 0 ta n
range 1
ta
n
range m ta n
Ta generate different random numbers at each
RND
use.
vou need ta have
sa
me
way of generating a random
seed.
(y
ou
can't cali
RND
ta doit!) A convenient
way of getting a pseudo-random
seed
on the
PET
is
ta use the current
jiffy
count.
TI:
10 X=RND(-TI):
REM
START
SEED
Here vou
will
get a different random sequence started each time statement
lOis
executed.
A more nearly pure random seed* can
be
obtained by using
RND
(-RND(O)).
For example:
10 X=RND(-RND(0»
REM
START
SEED
Here again vou
will
get a different random sequence started each time statement
lOis
executed.
ln the programs that follow.
-TI
is
used.
as
it
is
compatible
with
bath old and
new
ROMs.
If vou have the new
ROMs.
vou can use
-RND(O)
in place of
-TI.
Vou
will
have to convert the random number, which
is
returned
as
a
number
between
0 and
1,
to
whatever
range you
want
the random number to
be
in.
Say
vou
want
numbers from 1 ta 6
(as
in one die number of a dice game).
You
will
need ta multiply the random number by 6:
6:4ERND(1)
This gives a floating point number in a range
just
greater than 0
but
just
less
than
6
(O<n<6).
Add 1 ta get a number in the range 1
<n<7:
6:4ERND(
1)+1
Then convert the number ta integer.
which
discards any fractional part of a num-
ber. returning the number ta the range
1 ta 6
but
in integer form:
ItH<6:4ERND(
1
)+1)
or
A;';:
=6:4ERt·jIJ
( 1) +1
The general cases for converting the RND fraction to whole number
ranges are shown below. Note that
with
the INT function. these formulas handle
numbers in the integer range
±32767.
INT((n+1)'RND(1))
INT(n·RND(1)+
1)
INT((n-m+1
);RND(1
Hm
The formulas for ranges beginning at 0 or 1 are just simplified versions
derived directly from the general case of range m ta
n:
where m=O
where m=1
INT((n-m+1
)·RND(1 )+m)
INT((n-O+
1)'RND(1 )+0)
INT((n+1).RND(1))
as
shawn above
INT((n-m+1)·RND(1
Hm)
INT(n-1+1)·RND(1)+1)
INT(n.RND(1)+1)
as
shawn above
'according
to the
PET
manufacturer
293