Debian Codegraph @

%3a27/conftest.scm

raw symbols SCIP
1 ; CONFIDENCE TESTS FOR SRFI-27 "Sources of Random Bits"
2 ; =====================================================
3 ;
4 ; [email protected], 2002.
5 ;
6 ; This file contains a small collection of checks for the
7 ; implementation of SRFI-27. It is not meant to be complete
8 ; or to test the actual properties of the underlying generator.
9 ; It is merely meant to run the code and to check some of the
10 ; assumptions made by specification. There is an interface to
11 ; G. Marsaglia's DIEHARD battery of tests for random number
12 ; generators, though.
13 ;
14 ; running this file in Scheme 48 0.57 with SRFI-27 installed:
15 ;
16 ; ,open srfi-27 srfi-23 ascii
17 ; ,load conftest.scm
18 ;
19 ; speed in Scheme 48 0.57 with SRFI-27 opened:
20 ;
21 ; ,time (do ((k 0 (+ k 1))) ((= k 100000)) (random-integer 2))
22 ; ; about 30000/s on P3@800MHz for the Scheme-impl.
23 ; ; about 160000/s on P3@800MHz for the C/Scheme-impl.
24 ;
25 ; ,time (do ((k 0 (+ k 1))) ((= k 100000)) (random-real))
26 ; ; about 3000/s on P3@800MHz for the Scheme-impl.
27 ; ; about 180000/s on P3@800MHz for the C/Scheme-impl.
28 ;
29 ; running this file in Gambit 3.0 (interpreter) with SRFI-27 loaded:
30 ;
31 ; (define ascii->char integer->char)
32 ; (load "conftest.scm")
33 ;
34 ; compiling a simple program in Gambit 3.0 (compiler) using this file:
35 ;
36 ; 1. create 'a.scm' with content;
37 ; (include "srfi-27-c.scm")
38 ; (time (do ((k 0 (+ k 1))) ((= k 1000000)) (random-integer 2)))
39 ; (time (do ((k 0 (+ k 1))) ((= k 1000000)) (random-real)))
40 ; 2. compile Scheme into C (verbose paths for clarity)
41 ; GAMBCDIR=$GAMBIT/lib LD_LIBRARY_PATH=$GAMBIT/lib $GAMBIT/gsc/gsc a
42 ; 3. compile and link C code into an executable
43 ; gcc -I$GAMBIT/lib -o a a.c a_.c -lm -lgambc -L$GAMBIT/lib
44 ; 4. run the executable
45 ; GAMBCDIR=$GAMBIT/lib LD_LIBRARY_PATH=$GAMBIT/lib a
46 ;
47 ; speed in Gambit 3.0 with SRFI-27 loaded:
48 ;
49 ; (time (do ((k 0 (+ k 1))) ((= k 100000)) (random-integer 2)))
50 ; ; about 5000/s on P3@800MHz, interpreted
51 ; ; about 200000/s on P3@800MHz, compiled
52 ;
53 ; (time (do ((k 0 (+ k 1))) ((= k 100000)) (random-real)))
54 ; ; about 25000/s on P3@800MHz, interpreted
55 ; ; about 400000/s on P3@800MHz, compiled
56 ;
57 ; history of this file:
58 ; SE, 19-Mar-2002: initial version, based on earlier tests
59 ; SE, 22-Mar-2002: adapted to new procedure names
60 ; SE, 25-Mar-2002: more descriptive output
61 ; SE, 04-Apr-2002: some quick timings; check up
62
63
64 ; (check expr)
65 ; evals expr and issues an error if it is not #t.
66
67 (define (check expr)
68 (if (not (eq? (eval expr (interaction-environment)) #t))
69 (error "check failed" expr)))
70
71 ; Basic Tests of the Interface
72 ; ============================
73
74 (define (my-random-integer n)
75 (let ((x (random-integer n)))
76 (if (<= 0 x (- n 1))
77 x
78 (error "(random-integer n) returned illegal value" x))))
79
80 (define (my-random-real)
81 (let ((x (random-real)))
82 (if (< 0 x 1)
83 x
84 (error "(random-real) returned illegal value" x))))
85
86 (define (check-basics-1)
87
88 ; generate increasingly large numbers
89 (display "; generating large numbers [bits]: ")
90 (do ((k 0 (+ k 1))
91 (n 1 (* n 2)))
92 ((> k 1024))
93 (display k)
94 (display " ")
95 (my-random-integer n))
96 (display "ok")
97 (newline)
98
99 ; generate some reals
100 (display "; generating reals [1000 times]: ")
101 (do ((k 0 (+ k 1))
102 (x (my-random-real) (+ x (my-random-real))))
103 ((= k 1000)
104 x))
105 (display "ok")
106 (newline)
107
108 ; get/set the state
109 (display "; get/set state: ")
110 (let* ((state1 (random-source-state-ref default-random-source))
111 (x1 (my-random-integer (expt 2 32)))
112 (state2 (random-source-state-ref default-random-source))
113 (x2 (my-random-integer (expt 2 32))))
114 (random-source-state-set! default-random-source state1)
115 (let ((y1 (my-random-integer (expt 2 32))))
116 (if (not (= x1 y1))
117 (error "state get/set doesn't work" x1 y1 state1)))
118 (random-source-state-set! default-random-source state2)
119 (let ((y2 (my-random-integer (expt 2 32))))
120 (if (not (= x2 y2))
121 (error "state get/set doesn't work" x2 y2 state2))))
122 (display "ok")
123 (newline)
124
125 ; randomize!
126 (display "; randomize!: ")
127 (let* ((state1 (random-source-state-ref default-random-source))
128 (x1 (my-random-integer (expt 2 32))))
129 (random-source-state-set! default-random-source state1)
130 (random-source-randomize! default-random-source)
131 (let ((y1 (my-random-integer (expt 2 32))))
132 (if (= x1 y1)
133 (error "random-source-randomize! didn't work" x1 state1))))
134 (display "ok")
135 (newline)
136
137 ; pseudo-randomize!
138 (display "; pseudo-randomize!: ")
139 (let* ((state1 (random-source-state-ref default-random-source))
140 (x1 (my-random-integer (expt 2 32))))
141 (random-source-state-set! default-random-source state1)
142 (random-source-pseudo-randomize! default-random-source 0 1)
143 (let ((y1 (my-random-integer (expt 2 32))))
144 (if (= x1 y1)
145 (error "random-source-pseudo-randomize! didn't work" x1 state1)))
146 (random-source-state-set! default-random-source state1)
147 (random-source-pseudo-randomize! default-random-source 1 0)
148 (let ((y1 (my-random-integer (expt 2 32))))
149 (if (= x1 y1)
150 (error "random-source-pseudo-randomize! didn't work" x1 state1))))
151 (display "ok")
152 (newline)
153 (newline))
154
155
156 ; Testing the MRG32k3a Generator (if implemented)
157 ; ===============================================
158
159 ; (check-mrg32k3a)
160 ; tests if the underlying generator is the MRG32k3a generator
161 ; as implemented in the reference implementation. This function
162 ; is useful to check whether the reference implementation computes
163 ; the right numbers.
164
165 (define (check-mrg32k3a)
166
167 ; check if the initial state is A^16 * (1 0 0 1 0 0)
168 (display "; check A^16 * (1 0 0 1 0 0)")
169 (let* ((s (make-random-source))
170 (state1 (random-source-state-ref s))
171 (rand (random-source-make-reals s)))
172 (random-source-state-set! s '(lecuyer-mrg32k3a 1 0 0 1 0 0))
173 (do ((k 0 (+ k 1)))
174 ((= k 16)
175 (let ((state2 (random-source-state-ref s)))
176 (if (not (equal? state1 state2))
177 (error "16-th state after (1 0 0 1 0 0) is wrong"))))
178 (rand)))
179 (display "ok")
180 (newline)
181
182 ; check if pseudo-randomize! advances properly
183 (display "; checking (random-source-pseudo-randomize! s 1 2)")
184 (let ((s (make-random-source)))
185 (random-source-pseudo-randomize! s 1 2)
186 (if (not (equal? (random-source-state-ref s)
187 '(lecuyer-mrg32k3a
188 1250826159
189 3004357423
190 431373563
191 3322526864
192 623307378
193 2983662421)))
194 (error "pseudo-randomize! gives wrong result")))
195 (display "ok")
196 (newline)
197
198 ; run the check published by Pierre L'Ecuyer:
199 ; Note that the reference implementation deals slightly different
200 ; with reals mapping m1-1 into 1-1/(m1+1) and not into 0 as in
201 ; L'Ecuyer's original proposal. However, for the first 10^7 reals
202 ; that makes no difference as m1-1 is not generated.
203 (display "; checking (random-source-pseudo-randomize! s 1 2)...")
204 (let* ((x 0.0)
205 (s (make-random-source))
206 (rand (random-source-make-reals s)))
207 (random-source-state-set!
208 s
209 '(lecuyer-mrg32k3a 12345 12345 12345 12345 12345 12345))
210 (do ((k 0 (+ k 1)))
211 ((= k 10000000)
212 (if (not (< (abs (- x 5001090.95)) 0.01))
213 (error "bad sum over 10^7 reals" x)))
214 (set! x (+ x (rand)))))
215 (display "ok")
216 (newline))
217
218
219 ; Writing Data to DIEHARD
220 ; =======================
221
222 ; (write-diehard filename s bytes-per-call calls)
223 ; creates a binary file to which bytes-per-call * calls bytes are
224 ; written. The bytes are obtained from the random source s using
225 ; the range n = (expt 256 bytes-per-call).
226 ; The intention of write-diehard is to give implementors a
227 ; '15 min.'-way of running their favourite random number generator
228 ; through a pretty tough testsuite.
229 ;
230 ; try: For the reference implementation, the call
231 ;
232 ; (write-diehard "outfile" (make-random-source) 4 2867200)
233 ;
234 ; should create a file that looks as follows (od -A x -t x1 outfile):
235 ;
236 ; 0000000 92 bb 7e db 1b 14 f6 bb bb 54 a1 55 c2 3e cd ca
237 ; 0000010 23 01 20 35 06 47 65 b0 52 4c b8 c0 21 48 af 67
238 ; 0000020 63 a9 8c 78 50 73 29 08 62 d1 22 7f a6 89 96 77
239 ; 0000030 98 28 65 2d 2d 8b f9 52 41 be 8e 3f c5 84 0f ca
240 ; 0000040 c0 fa 03 d6 f0 65 9d 3a 9b ab 6f fe d1 aa 5f 92
241 ; 0000050 0f ea f6 3b 78 b9 fe ad 63 5e 49 f1 9d c9 8e 2f
242 ; 0000060 53 a9 5d 32 d4 20 51 1d 1c 2e 82 f0 8b 26 40 c0
243 ; ...total length is 11468800 bytes.
244 ;
245 ; The message digest is md5sum = 4df554f56cb5ed251bd04b0d50767443.
246 ;
247 ; try: For the reference implementation, the call
248 ;
249 ; (write-diehard "outfile" (make-random-source) 3 3822934)
250 ;
251 ; should create a file that looks as follows (od -A x -t x1 outfile):
252 ;
253 ; 000000 bb 7e db 30 a3 49 14 f6 bb d0 f2 d0 54 a1 55 8b
254 ; 000010 8c 03 3e cd ca a3 88 1d 01 20 35 e8 50 c8 47 65
255 ; 000020 b0 e7 d9 28 4c b8 c0 f2 82 35 48 af 67 42 3e 8a
256 ; 000030 a9 8c 78 12 ef b6 73 29 08 ff e9 71 d1 22 7f 52
257 ; 000040 b8 f0 89 96 77 dc 71 86 28 65 2d c2 82 fc 8b f9
258 ; 000050 52 d7 23 2a be 8e 3f 61 a8 99 84 0f ca 44 83 65
259 ; 000060 fa 03 d6 c2 11 c0 65 9d 3a c2 7a dd ab 6f fe 1c
260 ; ...total length is 11468802 bytes.
261 ;
262 ; The message digest is md5sum = 750ac219ff40c50bb2d04ff5eff9b24c.
263
264 (define (write-diehard filename s bytes-per-call calls)
265 (let ((port (open-output-file filename))
266 (rand (random-source-make-integers s))
267 (n (expt 256 bytes-per-call)))
268 (do ((i 0 (+ i 1)))
269 ((= i calls)
270 (close-output-port port))
271 (let ((x (rand n)))
272 (do ((k 0 (+ k 1))) ((= k bytes-per-call))
273 (write-char (ascii->char (modulo x 256)) port)
274 (set! x (quotient x 256)))))))
275
276 ; run some tests
277 (check-basics-1)
278 (display "passed (check-basics-1)")
279 (newline)
280