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
1994-06-24 14:02:31 +00:00
Version: $Id$
1988-02-19 15:54:01 +00:00
*)
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]
END Random;
1987-05-13 14:36:45 +00:00
PROCEDURE Uniform (lwb, upb: CARDINAL): CARDINAL;
BEGIN
1987-05-13 14:36:45 +00:00
IF upb <= lwb THEN RETURN lwb; END;
RETURN lwb + (Random() MOD (upb - lwb + 1));
END Uniform;
1987-05-13 14:36:45 +00:00
PROCEDURE StartSeed(seed: CARDINAL);
VAR v: CARDINAL;
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;
END StartSeed;
1988-02-19 15:54:01 +00:00
BEGIN
tm := time(NIL);
X[1] := tm;
StartSeed(CARDINAL(getpid()) * X[1]);
1987-05-13 14:36:45 +00:00
END random.