source: trunk/minix/lib/ack/libm2/random.mod@ 9

Last change on this file since 9 was 9, checked in by Mattia Monga, 14 years ago

Minix 3.1.2a

File size: 1.2 KB
Line 
1(*
2 (c) copyright 1988 by the Vrije Universiteit, Amsterdam, The Netherlands.
3 See the copyright notice in the ACK home directory, in the file "Copyright".
4*)
5
6(*$R-*)
7IMPLEMENTATION MODULE random;
8(*
9 Module: random numbers
10 Author: Ceriel J.H. Jacobs
11 Version: $Header: /cvsup/minix/src/lib/ack/libm2/random.mod,v 1.1 2005/10/10 15:27:46 beng Exp $
12*)
13
14 FROM Unix IMPORT getpid, time;
15 TYPE index = [1..55];
16
17 VAR X: ARRAY index OF CARDINAL;
18 j, k: index;
19 tm: LONGINT;
20
21 PROCEDURE Random(): CARDINAL;
22 BEGIN
23 IF k-1 <= 0 THEN k := 55; ELSE DEC(k) END;
24 IF j-1 <= 0 THEN j := 55; ELSE DEC(j) END;
25 X[k] := X[k] + X[j];
26 RETURN X[k]
27 END Random;
28
29 PROCEDURE Uniform (lwb, upb: CARDINAL): CARDINAL;
30 BEGIN
31 IF upb <= lwb THEN RETURN lwb; END;
32 RETURN lwb + (Random() MOD (upb - lwb + 1));
33 END Uniform;
34
35 PROCEDURE StartSeed(seed: CARDINAL);
36 VAR v: CARDINAL;
37 BEGIN
38 FOR k := 1 TO 55 DO
39 seed := 1297 * seed + 123;
40 X[k] := seed;
41 END;
42 FOR k := 1 TO 15 DO
43 j := tm MOD 55D + 1D;
44 v := X[j];
45 tm := tm DIV 7D;
46 j := tm MOD 55D + 1D;
47 X[j] := v;
48 tm := tm * 3D;
49 END;
50 k := 1;
51 j := 25;
52 END StartSeed;
53
54BEGIN
55 tm := time(NIL);
56 X[1] := tm;
57 StartSeed(CARDINAL(getpid()) * X[1]);
58END random.
Note: See TracBrowser for help on using the repository browser.