[net.micro.cpm] RANDOM in Pascal

ABN.ISCAMS%usc-isid@sri-unix.UUCP (12/12/83)

NetLandians,

I've been (grudgingly) trying to get the hang of Pascal, and have been playing
with my JRT Pascal 3.0 (yeah, yeah, I know -- no flames, please.)

Surprise, surprise:  NO Random.  Looked through my five reference books on
Pascal -- NO Random.  A friend tells me (and source code for a program also
contains it) that Pascal/MT+ does have it, but that's no help.

How, pray tell, can you do the usual Random(n) thing in Pascal?  I'm looking
for a fairly random series of numbers kind of like in BASIC; you know, the
decimal from .00something to .999something.

Since I haven't exactly figured out how to link Assembler programs into JRT
Pascal yet either, that kind of approach (grabbing a register's contents,
sequencing my way through memory) isn't practical yet (unless someone is
willing to expand on the JRT manual!).

Sure would appreciate the help.  I'm getting right at home in Assembler, but
Pascal is still somewhat foreign to me.

Regards, and thanks in advance.

David Kirschbaum
Toad Hall

ciaraldi@Rochester.ARPA (12/13/83)

From:  Mike Ciaraldi <ciaraldi@Rochester.ARPA>

I have seen a Pascal pseudo-random number generator prrogram.
I uses the well-know techinque of taking a seed number,
multiplying it by a constant, adding another constant, and
using the result as a random integer, and also as the next seed.
The problem is finding the "magic numbers" for the constants.
Someone has figured this out for various word lengths, and
I know a set of constants exists which causes a 16-bit
integer to cycle in a pseudo-random fashion through all 64k
numbers. This is then easily converted into a fraction
between 0 and 1.0.
The algorithm I saw was written up in some sample programs
which came with the Motorola Pascal compiler (produces
6809 code on an IBM 360).
I think the magic numbers are listed in the
"Encyclopedia of Computer Science", edited by Anthony Ralston.
You might also try the ACM collected Algortiithms.

Mike Ciaraldi
ciaraldi@rochester

Eldridge.es@PARC-MAXC.ARPA (12/13/83)

Try the following (I use it in JRT Pascal):

function Random(Seed : real): real;

{ Return the next value in a pseudo-random sequence.  The range is
0<=N<1. }

begin
  Seed := (9821.0 * Seed) + 0.211327;
  Seed := Seed - trunc(Seed);
  Random := Seed
end;  {Random}


Initialize the random number generator by giving it a first seed.
(RandNum and StartValue are both real)

  RandNum := Random(StartValue);

Succeeding numbers in the sequence are generated by:

  RandNum := Random(RandNum);


The formula used is:  NextSeed = frac((9821.0 * Seed) + 0.211327)
This is the formula in the HP-41C standard applications library.

George