-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathRandom.Mod
More file actions
51 lines (44 loc) · 1.2 KB
/
Random.Mod
File metadata and controls
51 lines (44 loc) · 1.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
MODULE Random;
(** Lehmer multiplicative linear congruential generator (MLCG) for INTEGER and REAL random numbers. *)
CONST
Modulus* = 2147483647; (* 2^31-1, a Mersenne prime *)
Multiplier* = 48271; (* Park-Miller MINSTD *)
VAR
state: INTEGER;
PROCEDURE Init*(seed: INTEGER);
(** Initialize the generator with a seed in 1..Modulus-1 *)
BEGIN
IF (seed > 0) & (seed < Modulus) THEN
state := seed
ELSE
state := 1
END
END Init;
PROCEDURE Next*(): INTEGER;
(** Return the next random integer in 1..Modulus-1 *)
VAR
result, hi, lo, test: INTEGER;
BEGIN
(* Schrage's method: computes (Multiplier * state) MOD Modulus without overflow. *)
hi := state DIV 44488; (* q = Modulus DIV Multiplier = 44488 *)
lo := state MOD 44488; (* r = Modulus MOD Multiplier = 3399 *)
test := Multiplier * lo - 3399 * hi;
IF test > 0 THEN
state := test
ELSE
state := test + Modulus
END;
result := state;
RETURN result
END Next;
PROCEDURE NextReal*(): REAL;
(** Return the next random number in (0,1) *)
VAR
result: REAL;
n: INTEGER;
BEGIN
n := Next();
result := FLT(n) / FLT(Modulus);
RETURN result
END NextReal;
END Random.