raz / lib / Rand.raz

97 lines · 3.2 KiB · raw · history · last change 280d4d6f

1(* A seeded random number generator, splitmix64: one U64 of state, a
2 few steps to a number. The same seed gives the same numbers, on
3 every machine, so a failing run can be replayed. It is fast and
4 passes the usual statistical tests, but it is not for secrets.
5 Needs Sys (for a fresh seed) and Rt; its test uses Check.
6 Leaves out reals, shuffles and other distributions. *)
7module Rand;
8import Sys, Rt, Check;
9
10type
11 Rand* = record
12 state: U64; (* advances by a constant at each draw *)
13 end;
14
15(* Starts r at seed; any seed, 0 too, will do. *)
16proc init*(var r: Rand; seed: U64);
17begin
18 r.state := seed;
19end;
20
21(* The next number, all 64 bits of it. *)
22proc next*(var r: Rand): U64;
23var z: U64;
24begin
25 INC(r.state, 09E3779B97F4A7C15H);
26 z := r.state;
27 z := (z xor (z shr 30)) * 0BF58476D1CE4E5B9H;
28 z := (z xor (z shr 27)) * 094D049BB133111EBH;
29 return z xor (z shr 31);
30end;
31
32(* A number from 0 to n - 1, each as likely as another. It draws again
33 when it falls in the short last run of the 64-bit range, which n
34 does not divide, so no number is favoured. n = 0 stops the program. *)
35proc below*(var r: Rand; n: U64): U64;
36var x, short: U64;
37begin
38 if n = 0 then Rt.stop("Rand.below: n is 0"); end;
39 short := (0 - n) mod n; (* 2^64 mod n numbers are left over *)
40 x := next(r);
41 while x < short do x := next(r); end;
42 return x mod n;
43end;
44
45(* A number from lo to hi, both included; lo must not exceed hi. The
46 span is taken as a U64, so the whole I64 range works. *)
47proc range*(var r: Rand; lo, hi: I64): I64;
48var span: U64;
49begin
50 if lo > hi then Rt.stop("Rand.range: lo > hi"); end;
51 span := U64(hi) - U64(lo);
52 if span = 0FFFFFFFFFFFFFFFFH then return I64(next(r)); end;
53 return I64(U64(lo) + below(r, span + 1));
54end;
55
56(* A fresh seed from the kernel, for a program to print and, to replay
57 a run, to be given back to init. *)
58proc seed*(): U64;
59var b: [8]U8; i: INT; s: U64;
60begin
61 Sys.random(b);
62 s := 0;
63 for i := 0 to 7 do s := s shl 8 | U64(b[i]); end;
64 return s;
65end;
66
67(* The module's test. *)
68proc main*;
69var r, q: Rand; i: INT; x: U64; v: I64; lo, hi: BOOL;
70begin
71 init(r, 0);
72 Check.ok(next(r) = 0E220A8397B1DCDAFH, "Rand.next: splitmix64 from 0, first");
73 Check.ok(next(r) = 06E789E6AA1B965F4H, "Rand.next: splitmix64 from 0, second");
74 Check.ok(next(r) = 006C45D188009454FH, "Rand.next: splitmix64 from 0, third");
75 init(r, 42); init(q, 42);
76 for i := 1 to 100 do
77 Check.ok(next(r) = next(q), "Rand: same seed, different numbers");
78 end;
79 for i := 1 to 1000 do
80 Check.int("Rand.below 1", I64(below(r, 1)), 0);
81 Check.ok(below(r, 3) < 3, "Rand.below 3");
82 x := below(r, 08000000000000001H);
83 Check.ok(x < 08000000000000001H, "Rand.below 2^63+1");
84 end;
85 lo := false; hi := false;
86 for i := 1 to 1000 do
87 v := range(r, -2, 2);
88 Check.ok((v >= -2) and (v <= 2), "Rand.range -2..2");
89 lo := lo or (v = -2); hi := hi or (v = 2);
90 end;
91 Check.ok(lo and hi, "Rand.range: ends not reached");
92 for i := 1 to 100 do v := range(r, -9223372036854775807 - 1, 9223372036854775807); end;
93 Check.int("Rand.range 7..7", range(r, 7, 7), 7);
94 x := seed();
95end;
96
97end Rand.