ack/lang/m2/libm2/random.mod

59 lines
1.1 KiB
Modula-2
Raw Normal View History

1988-02-19 15:54:01 +00:00
(*
(c) copyright 1988 by the Vrije Universiteit, Amsterdam, The Netherlands.
See the copyright notice in the ACK home directory, in the file "Copyright".
*)
1987-08-19 18:07:01 +00:00
(*$R-*)
1987-05-13 14:36:45 +00:00
IMPLEMENTATION MODULE random;
1988-02-19 15:54:01 +00:00
(*
Module: random numbers
Author: Ceriel J.H. Jacobs
Version: $Header$
*)
1987-05-13 14:36:45 +00:00
FROM Unix IMPORT getpid, time;
TYPE index = [1..55];
VAR X: ARRAY index OF CARDINAL;
j, k: index;
tm: LONGINT;
1987-05-13 14:36:45 +00:00
PROCEDURE Random(): CARDINAL;
BEGIN
IF k-1 <= 0 THEN k := 55; ELSE DEC(k) END;
IF j-1 <= 0 THEN j := 55; ELSE DEC(j) END;
X[k] := X[k] + X[j];
RETURN X[k]
1987-05-13 14:36:45 +00:00
END Random;
PROCEDURE Uniform (lwb, upb: CARDINAL): CARDINAL;
BEGIN
IF upb <= lwb THEN RETURN lwb; END;
RETURN lwb + (Random() MOD (upb - lwb + 1));
END Uniform;
1988-02-19 15:54:01 +00:00
PROCEDURE StartSeed(seed: CARDINAL);
VAR v: CARDINAL;
1987-05-13 14:36:45 +00:00
BEGIN
FOR k := 1 TO 55 DO
seed := 1297 * seed + 123;
X[k] := seed;
END;
FOR k := 1 TO 15 DO
j := tm MOD 55D + 1D;
v := X[j];
tm := tm DIV 7D;
j := tm MOD 55D + 1D;
X[j] := v;
tm := tm * 3D;
END;
k := 1;
j := 25;
1988-02-19 15:54:01 +00:00
END StartSeed;
BEGIN
tm := time(NIL);
X[1] := tm;
StartSeed(CARDINAL(getpid()) * X[1]);
1987-05-13 14:36:45 +00:00
END random.