Category : Modula II Source Code
Archive   : TASKING.ZIP
Filename : RANDOMNU.MOD

 
Output of file : RANDOMNU.MOD contained in archive : TASKING.ZIP
IMPLEMENTATION MODULE RandomNumbers;

(*
Additive Number Generator
from Knuth, Seminumerical Algorithms, 2nd ed. p. 27, 171
*)

FROM TimeDate IMPORT GetTime, Time;
FROM SYSTEM IMPORT SETREG, GETREG, CODE, AX, BX;

CONST
base = 32700;

VAR
IA: ARRAY [1 .. 55] OF INTEGER;
JRAND: CARDINAL;
time: Time;

PROCEDURE srand(IX: INTEGER);
VAR
I, II: INTEGER;
J, K: INTEGER;
BEGIN
IX := IX MOD base;
IF IX < 0 THEN IX := - IX END;
IA[55] := IX;
J := IX;
K := 1;
FOR I := 1 TO 54 DO
II := 21 * I MOD 55;
IA[II] := K;
K := J - K;
IF K < 0 THEN
INC(K, base);
END;
J := IA[II];
END;
IRN; (* rev it up as D.K. sayes *)
IRN;
IRN;
END srand;

PROCEDURE irand(limit: INTEGER): INTEGER ;
VAR
val: INTEGER;
BEGIN
INC(JRAND);
IF JRAND > 55 THEN
IRN;
END;
(*
RETURN TRUNC(FLOAT(IA[JRAND]) * FLOAT(limit) / FLOAT(base));
*)
val := IA[JRAND];
SETREG(AX, limit);
SETREG(BX, val);
CODE(0F7H, 0E3H); (* MULW BX *)
SETREG(BX, base);
CODE(0F7H, 0F3H); (* DIVW BX *)
GETREG(AX, val);
RETURN val;
END irand;

PROCEDURE IRN;
VAR
i: INTEGER;
j: INTEGER;
BEGIN
FOR i := 1 TO 24 DO
j := IA[i] - IA[i + 31];
IF j < 0 THEN
INC(j, base);
END;
IA[i] := j;
END;
FOR i := 25 TO 55 DO
j := IA[i] - IA[i - 24];
IF j < 0 THEN
INC(j, base);
END;
IA[i] := j;
END;
JRAND := 1;
END IRN;

BEGIN
GetTime(time);
(*$R-*)
srand(VAL(INTEGER, time.day + time.minute + time.millisec));
(*$R=*)
END RandomNumbers.


  3 Responses to “Category : Modula II Source Code
Archive   : TASKING.ZIP
Filename : RANDOMNU.MOD

  1. Very nice! Thank you for this wonderful archive. I wonder why I found it only now. Long live the BBS file archives!

  2. This is so awesome! 😀 I’d be cool if you could download an entire archive of this at once, though.

  3. But one thing that puzzles me is the “mtswslnkmcjklsdlsbdmMICROSOFT” string. There is an article about it here. It is definitely worth a read: http://www.os2museum.com/wp/mtswslnk/