ack/lang/m2/libm2/random.mod

48 lines
954 B
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 = [0..54];
VAR X: ARRAY index OF CARDINAL;
k, j: index;
1987-05-13 14:36:45 +00:00
PROCEDURE Random(): CARDINAL;
BEGIN
IF k+1 > 54 THEN k := 0; ELSE INC(k) END;
IF j+1 > 54 THEN j := 0; ELSE INC(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);
1987-05-13 14:36:45 +00:00
BEGIN
1988-02-19 15:54:01 +00:00
X[0] := seed;
FOR k := 1 TO 54 DO
X[k] := X[k-1] * 1297;
END;
k := 54;
j := 30;
1988-02-19 15:54:01 +00:00
END StartSeed;
BEGIN
1988-02-19 16:36:45 +00:00
X[0] := time(NIL);
1988-02-19 15:54:01 +00:00
StartSeed(CARDINAL(getpid()) * X[0]);
1987-05-13 14:36:45 +00:00
END random.