[Coco] Pascal random() for OS-9 and DEFT
Willard Goosey
goosey at virgo.sdc.org
Sun Jan 24 00:22:10 EST 2010
I've finally finished one of my little projects!
Neither of the Pascal compilers I have for the CoCo have a random()
function. This is annoying, because I'd like to port a few of my
High-School era Turbo PASCAL games. I am a firm believer in using
random numbers for games! (ROGUE rul3z, DOOM dr00lz! :-)
So I finally had a chance to poke around in the college library, and
found _Numerical Recipes in Pascal_ by William H. Press and others.
He also has this book in C and FORTRAN! It had a nice little
uniform-distribution pseudo-random number generator.
Here is 3 versions of it: OS-9 Pascal subroutine module, OS-9 Pascal
using "include", and DEFT Pascal under DECB.
All 3 versions are in http://www.sdc.org/~goosey/os9/random.lzh
Only the DEFT version in http:www.sdc.org/~goosey/coco/random.arc
which is a TCarc file.
The OS-9 subroutine module version was the first one I got to work.
Unfortunately, it's the most painful to work with. This algorithm
requires a significant amount of static storage, and that is simply
not available for OS-9 subroutine modules (of any language). This
means all of its "static" variables must be stored in the global
PROGRAM and passed as arguments. Yuck. The subroutine module random
is included in this archive.
The "Include" version makes use of the INCLUDE utility on rtsi.
Simply place a line that says '#include "rnd3.var"' in the variable
section of the PROGRAM, and a line that says '#include "rnd3.proc"' in
the procedure section. There is also a rnd3int.proc that returns
integers instead of reals that can be included if desired.
The DEFT Pascal version is used by placing a "%C RANDOM/EXT" above
your PROGRAM or MODULE line to include the INTERFACE module, and then
linking in RANDOM/OBJ with the linker. DEFT source and RANDOM/OBJ is
included in the TC archive.
Makefiles and other automata: Also included are the makefiles used to
generate OS-9 executables, and my customized make.default file for the
much improved make_TK on rtsi.
The main reason the subroutine version is included here is as an
example of automating the Pascal compile/link process.
There is no real way to automate the DEFT Pascal compilation process.
Test programs:
All three versions have a version of testr. This test program was
mainly to make sure the PROGRAM's variable weren't being clobbered by
the generator's variables.
The DEFT and OS-9 include versions have a chi-square test program
chisquar. This tests how evenly the generator distributes the numbers
it generates. It is based on code from _Algorithms in C++_ by Robert
Sedgewick. A generator passes Sedgewick's "crude" chi-square test if
the result is within 2*sqrt(die size). (See Knuth for a far-more
detailed version of the chi-square test.)
Both the OS-9 and DEFT versions pass this test nicely. DECB's rnd(),
btw, passes 9 out of 10 trials of 10,000 d100's.
The Chi-Square tests were done, not because I doubt Knuth, but because
I wanted to make sure the given constants, designed for 32-bit reals,
were still good on the 40-bit reals of OS-9 Pascal and the 48-bit
reals of DEFT. They are.
Conclusion: Under OS-9, use the "include" based version.
There's nothing like Pascal to make one appreciate Superior
languages, like BASIC, C, and Java! ;-)
Willard
--
Willard Goosey goosey at sdc.org
Socorro, New Mexico, USA
I search my heart and find Cimmeria, land of Darkness and the Night.
-- R.E. Howard
--
Willard Goosey goosey at sdc.org
Socorro, New Mexico, USA
I search my heart and find Cimmeria, land of Darkness and the Night.
-- R.E. Howard
More information about the Coco
mailing list