Random Numbers

Hardware Hacking, Programming and Game Solutions/Cheats
Post Reply
zephyr
Posts: 1474
Joined: Mon Jul 21, 2008 1:18 am

Random Numbers

Post by zephyr »

Here's a handy tip from Brian Cadge. This was copied from the "Dragon Answers" page in the January 1984 issue of Dragon User.
Attachments
RANDOM.png
RANDOM.png (53.46 KiB) Viewed 3989 times
zephyr
Posts: 1474
Joined: Mon Jul 21, 2008 1:18 am

Re: Random Numbers

Post by zephyr »

More detailed information by Norm Barson (copied from COCO FRIENDS DISK MAGAZINE Issue #27).
RANDOMIZATION ON THE COCO
Norm Barson

Rick's program RANDOMWN in Issue 24 gave me the
idea of writing a brief article on how random num-
bers can be generated and used on the CoCo (1,2,3).
The function RND can be used in several different
ways: RND(0) will return a number between 0 and 1
(but, excluding 0 and 1). RND(X), where X is an
integer, will return an integer between 1 and X.
However, there is a catch. The CoCo has a built-in
table of "pseudo" random numbers. That is, every
time you use the RND(X) function after a cold or a
warm start, you will start at the beginning of the
table and get the same number (or series of
numbers). The rationale behind this is to make
debugging easier, but it defeats the purpose of
randomization if it is desired in a program.

It is possible to "reseed" the random number
generator by entering the command ZZ=RND(-X) where
X is any number and ZZ is a dummy variable that is
never used. Even this technique is not perfect since
every time you use the same -X, you will go to the
same place in the table of random numbers and return
the same number. Out of curiosity, I tried generat-
ing a list of the values of RND(-X) where X varied
from 1 to 25. There were a lot of groups of identi-
cal numbers generated. For example, X=1, 2, 4, 8,
16, etc., all gave the same result; X=3 and 6 gave
another number. Most of the numbers generated ended
in 2849 or 285, 22849 or 2285, 222849 or 22285. I
haven't figured out why this happens but suspect it
has to do with the binary form of the integer X.
Furthermore, whenever the value of the RND(-X) was
the same, subsequent random numbers all started
at the same point in the random number table. This
is still not satisfactory for generating truly
random numbers.

A technique often used (other than that used by
Rick) is to enter the command ZZ=RND(-TIMER), where
ZZ is again a dummy variable. When the computer is
turned on, an internal timer starts which measures
time in sixtieths of a second (approximately). When
it reaches 65535 (about 18 minutes later), it begins
at zero again. Using the negative value of TIMER to
seed the random number generator makes it virtually
impossible to start at the same point in the table,
thus giving truly random numbers each time.


RANDOMIZATION ON THE COCO (PT 2)
Norm Barson

I have seen it argued that it is preferable to
use RND(-TIMER) only once -- as opposed to repeating
it before each call for another random number. I
have no opinion on whether this is correct or not.
But, if you assume that the random number table is a
sequence of truly random numbers (as it supposedly
is), then entering the table at any point and taking
what you get from then on should be adequate.
jedie
Posts: 655
Joined: Wed Aug 14, 2013 12:23 pm
Location: germany
Contact:

Re: Random Numbers

Post by jedie »

Nice information, related to this: viewtopic.php?f=8&t=4894

Think the Wiki is a better place for it!
... too many ideas and too little time ... Related stuff written in Python:
Dragon 32 emulator / PyDC - Python Dragon 32 converter: https://github.com/jedie/DragonPy
DWLOAD server / Dragon-Lib and other stuff: https://github.com/6809
Post Reply