File: test-random.scm

package info (click to toggle)
mit-scheme 12.1-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 208,300 kB
  • sloc: lisp: 781,881; xml: 425,435; ansic: 86,059; sh: 10,135; makefile: 2,501; asm: 2,121; csh: 1,143
file content (224 lines) | stat: -rw-r--r-- 7,594 bytes parent folder | download | duplicates (2)
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
#| -*-Scheme-*-

Copyright (C) 1986, 1987, 1988, 1989, 1990, 1991, 1992, 1993, 1994,
    1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
    2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016,
    2017, 2018, 2019, 2020, 2021, 2022 Massachusetts Institute of
    Technology

This file is part of MIT/GNU Scheme.

MIT/GNU Scheme is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or (at
your option) any later version.

MIT/GNU Scheme is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
General Public License for more details.

You should have received a copy of the GNU General Public License
along with MIT/GNU Scheme; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301,
USA.

|#

;;;; Tests of random number generator

(declare (usual-integrations))

(define-test 'random-state
  (lambda ()
    (assert-true (random-state? (make-random-state)))))

(define-test 'random-source
  (lambda ()
    (assert-true (random-source? (make-random-source)))))

(define-test 'random-state-fresh
  (lambda ()
    (let* ((s0 (make-random-state #t))
	   (s1 (make-random-state #t)))
      (assert-!= ((random-source-make-integers s0) (expt 2 32))
		 ((random-source-make-integers s1) (expt 2 32))))))

(define-test 'random-source-fresh-deterministic
  (lambda ()
    (let* ((s0 (make-random-source))
	   (s1 (make-random-source)))
      (assert-= ((random-source-make-integers s0) (expt 2 64))
		((random-source-make-integers s1) (expt 2 64))))))

(define-test 'random-source-randomize!
  (lambda ()
    (let* ((s0 (make-random-state #t))
	   (s1 (make-random-state s0)))
      (random-source-randomize! s0)
      (assert-!= ((random-source-make-integers s0) (expt 2 32))
		 ((random-source-make-integers s1) (expt 2 32))))))

(define-test 'random-bytevector-large
  (lambda ()
    (assert-= (bytevector-length (random-bytevector 1088)) 1088)))

(define (define-random-test name procedure)
  (define-test name
    (lambda ()
      ;; XXX Should make the seed more compact than just the original
      ;; complete state.
      (let ((state (make-random-state #t)))
	(with-test-properties
	    (lambda ()
	      ;; Ensure we don't accidentally use the global state.
	      (fluid-let ((*random-state* 'loser-state)
			  (default-random-source 'loser-source))
		(procedure state)))
	  'seed (export-random-state state))))))

(define-random-test 'random-state-derived
  (lambda (state)
    (let* ((s0 (make-random-state state))
	   (s1 (make-random-state s0)))
      (assert-true (random-state? s0))
      (assert-true (random-state? s1))
      (assert-= ((random-source-make-integers s0) (expt 2 64))
		((random-source-make-integers s1) (expt 2 64))))))

(define-random-test 'random-source-ref/set!
  (lambda (state)
    (let* ((s0 state)
	   (s1 (make-random-state #t)))
      (random-source-state-set! s1 (random-source-state-ref s0))
      (assert-= ((random-source-make-integers s0) (expt 2 64))
		((random-source-make-integers s1) (expt 2 64))))))

(define-random-test 'import/export
  (lambda (state)
    (let* ((s0 (make-random-state state))
	   (s1 (import-random-state (export-random-state s0))))
      (assert-= ((random-source-make-integers s0) (expt 2 64))
		((random-source-make-integers s1) (expt 2 64))))))

(define-random-test 'random/integer
  (lambda (state)
    (let ((ok (make-vector 3)))
      (do ((i 0 (+ i 1)))
	  ((>= i 300))
	(vector-set! ok (random 3 state) #t))
      (do ((i 0 (+ i 1)))
	  ((>= i 3))
	(assert-true (vector-ref ok i))))))

(define-random-test 'random/float
  (lambda (state)
    (do ((i 0 (+ i 1)))
	((>= i 64))
      (let ((x (random 0.25 state)))
	((predicate-assertion flo:flonum? "flonum") x)
	(assert-true (<= 0 x 0.25))))))

(define-random-test 'random/rational
  (lambda (state)
    (assert-error (lambda () (random 1/4 state)))))

(define-random-test 'random-bytevector!/short
  (lambda (state)
    (let ((bv (make-bytevector 32 0))
          (state* (make-random-state state)))
      (random-bytevector! bv 3 20 state)
      (assert-equal (bytevector-copy bv 0 3) (make-bytevector 3 0))
      (assert-equal (bytevector-copy bv 3 20) (random-bytevector 17 state*))
      (assert-equal (bytevector-copy bv 20 32) (make-bytevector  12 0)))))

(define-random-test 'random-bytevector!/long
  (lambda (state)
    (let ((bv (make-bytevector 3000 0))
          (state* (make-random-state state)))
      (random-bytevector! bv 1000 2000 state)
      (assert-equal (bytevector-copy bv 0 1000) (make-bytevector 1000 0))
      (assert-equal (bytevector-copy bv 1000 2000)
                    (random-bytevector 1000 state*))
      (assert-equal (bytevector-copy bv 2000 3000) (make-bytevector 1000 0)))))

;;; Stochastic tests

(define NSAMPLES 100000)
(define NTRIALS 2)
(define NPASSES-MIN 1)

(define PSI-DF 100)			;degrees of freedom
(define PSI-CRITICAL 135.807)		;critical value, alpha = .01

(define (psi-test counts logps n)
  (let loop ((i 0) (psi 0.) (c 0.))
    (if (< i PSI-DF)
	(let ((count (vector-ref counts i))
	      (logp (vector-ref logps i)))
	  (if (= count 0)
	      (loop (+ i 1) psi c)
	      (let* ((t (* count (- (log (/ count n)) logp)))
		     (t* (- t c))
		     (psi* (+ psi t*))
		     (c* (- (- psi* psi) t*)))
		(loop (+ i 1) psi* c*))))
	(<= (* 2 psi) PSI-CRITICAL))))

(define (count-sample nsamples procedure)
  (let ((counts (make-vector PSI-DF 0)))
    (do ((i 0 (+ i 1)))
	((>= i nsamples))
      (let ((bin (procedure)))
	(vector-set! counts bin (+ 1 (vector-ref counts bin)))))
    counts))

(define (assert-psi-test nsamples logps procedure)
  ;; Square the false positive rate for each test case (alpha=.01 --->
  ;; alpha=.0001) so that the false positive rate for the whole test
  ;; suite isn't too high.
  (assert-true
   (or (psi-test (count-sample nsamples procedure) logps nsamples)
       (psi-test (count-sample nsamples procedure) logps nsamples))))

(define-random-test 'uniform-float01
  (lambda (state)
    (let ((logps (make-vector PSI-DF (log (/ 1 PSI-DF)))))
      (assert-psi-test NSAMPLES logps
	(lambda ()
	  (min (- PSI-DF 1)
	       (floor->exact (* PSI-DF (flo:random-unit state)))))))))

(define-random-test 'uniform-integer
  (lambda (state)
    (let ((random-integer (random-source-make-integers state))
	  (logps (make-vector PSI-DF (log (/ 1 PSI-DF)))))
      (assert-psi-test NSAMPLES logps
	(lambda ()
	  (random-integer PSI-DF))))))

;;; Confirm that this simple-minded psi test has adequate statistical
;;; power to detect a modulo bias.

(define-random-test 'nonuniform-integer
  (lambda (state)
    (let ((random-integer (random-source-make-integers state))
	  (logps (make-vector PSI-DF (log (/ 1 PSI-DF)))))
      (define (sampler)
	(modulo (random-integer (+ 1 (* 2 PSI-DF))) PSI-DF))
      (assert-false
       (psi-test (count-sample nsamples sampler) logps nsamples)))))

(define-random-test 'geometric-1/2
  (lambda (state)
    (let ((random-integer (random-source-make-integers state))
	  (logps
	   (make-initialized-vector PSI-DF
	     (lambda (k)
	       ;; p = 1/2, so p = 1 - p, so (1 - p)^{k - 1} * p = p^k.
	       (* k (log 1/2))))))
      (assert-psi-test NSAMPLES logps
	(lambda ()
	  (min (- PSI-DF 1)
	       ;; Probability of zero is 1/2^1000 = never.
	       (first-set-bit (random-integer (shift-left 1 1000)))))))))