[comp.sys.apple] Random numbers in TML Pascal

cc@xroads.UUCP (Dan McGuirk) (03/09/88)

Does anyone know of a way to generate a random number in TML Pascal?  There
appears to be no 'random' function, and I can't find anything in the manual
about it..[or about anything, for that matter..the manual seems to be pretty
worthless to me]  Is there a command built in to TML to do this or am I going
to have to use a completely external procedure?

Thanks in advance..

Dan McGuirk
ihnp4!crash!xroads!cc
-- 
\  /  C r o s s r o a d s  C o m m u n i c a t i o n s
 \/   (602) 971-2240
 /\   (602) 992-5007 300|1200 Baud 24 hrs/day
/  \  ihnp4!crash!xroads!*

douglas@reed.UUCP (P Douglas Reeder) (03/11/88)

You can easily write your own, if you like, with the following equations:

seed := (seed*a + c) mod m
random := seed/m

One good set of numbers to use is: a=62605,c=113218009,m=536870912
A good first seed is 7774755.


-- 
Doug Reeder            USENET: ...!tektronix!reed!douglas
Box 502 Reed College   BITNET: douglas@reed.UUCP
3203 S.E. Woodstock      ARPA: !tektronix!reed!douglas@Berkley
Portland, OR 97202   UNIVERSE: Local Group,Milky Way,Sol III,122 38'W 45 28'N

tribby@hpindda.HP.COM (David Tribby) (03/12/88)

/ cc@xroads.UUCP (Dan McGuirk) /  writes...
> Does anyone know of a way to generate a random number in TML Pascal?

There are two random number tools built into the IIGS:

    Random  is an integer function with no parameters that returns
            values in the range -32768 to 32767.

    SetRandSeed(randomSeed)  sets the seed (a longInt) for the random number
            algorithm. (It uses a 32-bit seed to produce a 16-bit number.)

The sequence of numbers generated by Random depends upon the seed value.
If you use the same seed in different runs of your program, you will get the
same sequence of pseudo-random numbers.

These tools are located--of all places--in QuickDraw II. Before calling them,
you have to load QuickDraw II, allocate memory for its direct page, and
start it up. Don't forget to shut down at the end of the program.

On a related subject... The examples shipped with TML Pascal do not set the
"align on a page" bit when they allocate memory for tools' direct page. This
is not always a problem, but sometimes causes very strange results. Be sure
you always set that bit in your own programs.

When TML Pascal's library starts up SANE for you, it does not set the page
alignment bit. One of my programs randomly crashed until I modified it to
shut down SANE, allocate a page-aligned block of memory, then restart
SANE. This is not a problem unless you are using Real numbers.

--Dave Tribby
                                   - - - - -
        ARPA: tribby%hpda@hplabs.HP.COM   UUCP: hplabs!hpda!tribby