Debian Codegraph @

%3a27/mrg32k3a.scm

raw symbols SCIP
1 ; GENERIC PART OF MRG32k3a-GENERATOR FOR SRFI-27
2 ; ==============================================
3 ;
4 ; [email protected], 2002.
5 ;
6 ; This is the generic R5RS-part of the implementation of the MRG32k3a
7 ; generator to be used in SRFI-27. It is based on a separate implementation
8 ; of the core generator (presumably in native code) and on code to
9 ; provide essential functionality not available in R5RS (see below).
10 ;
11 ; compliance:
12 ; Scheme R5RS with integer covering at least {-2^53..2^53-1}.
13 ; In addition,
14 ; SRFI-23: error
15 ;
16 ; history of this file:
17 ; SE, 22-Mar-2002: refactored from earlier versions
18 ; SE, 25-Mar-2002: pack/unpack need not allocate
19 ; SE, 27-Mar-2002: changed interface to core generator
20 ; SE, 10-Apr-2002: updated spec of mrg32k3a-random-integer
21
22 ; Generator
23 ; =========
24 ;
25 ; Pierre L'Ecuyer's MRG32k3a generator is a Combined Multiple Recursive
26 ; Generator. It produces the sequence {(x[1,n] - x[2,n]) mod m1 : n}
27 ; defined by the two recursive generators
28 ;
29 ; x[1,n] = ( a12 x[1,n-2] + a13 x[1,n-3]) mod m1,
30 ; x[2,n] = (a21 x[2,n-1] + a23 x[2,n-3]) mod m2,
31 ;
32 ; where the constants are
33 ; m1 = 4294967087 = 2^32 - 209 modulus of 1st component
34 ; m2 = 4294944443 = 2^32 - 22853 modulus of 2nd component
35 ; a12 = 1403580 recursion coefficients
36 ; a13 = -810728
37 ; a21 = 527612
38 ; a23 = -1370589
39 ;
40 ; The generator passes all tests of G. Marsaglia's Diehard testsuite.
41 ; Its period is (m1^3 - 1)(m2^3 - 1)/2 which is nearly 2^191.
42 ; L'Ecuyer reports: "This generator is well-behaved in all dimensions
43 ; up to at least 45: ..." [with respect to the spectral test, SE].
44 ;
45 ; The period is maximal for all values of the seed as long as the
46 ; state of both recursive generators is not entirely zero.
47 ;
48 ; As the successor state is a linear combination of previous
49 ; states, it is possible to advance the generator by more than one
50 ; iteration by applying a linear transformation. The following
51 ; publication provides detailed information on how to do that:
52 ;
53 ; [1] P. L'Ecuyer, R. Simard, E. J. Chen, W. D. Kelton:
54 ; An Object-Oriented Random-Number Package With Many Long
55 ; Streams and Substreams. 2001.
56 ; To appear in Operations Research.
57 ;
58 ; Arithmetics
59 ; ===========
60 ;
61 ; The MRG32k3a generator produces values in {0..2^32-209-1}. All
62 ; subexpressions of the actual generator fit into {-2^53..2^53-1}.
63 ; The code below assumes that Scheme's "integer" covers this range.
64 ; In addition, it is assumed that floating point literals can be
65 ; read and there is some arithmetics with inexact numbers.
66 ;
67 ; However, for advancing the state of the generator by more than
68 ; one step at a time, the full range {0..2^32-209-1} is needed.
69
70
71 ; Required: Backbone Generator
72 ; ============================
73 ;
74 ; At this point in the code, the following procedures are assumed
75 ; to be defined to execute the core generator:
76 ;
77 ; (mrg32k3a-pack-state unpacked-state) -> packed-state
78 ; (mrg32k3a-unpack-state packed-state) -> unpacked-state
79 ; pack/unpack a state of the generator. The core generator works
80 ; on packed states, passed as an explicit argument, only. This
81 ; allows native code implementations to store their state in a
82 ; suitable form. Unpacked states are #(x10 x11 x12 x20 x21 x22)
83 ; with integer x_ij. Pack/unpack need not allocate new objects
84 ; in case packed and unpacked states are identical.
85 ;
86 ; (mrg32k3a-random-range) -> m-max
87 ; (mrg32k3a-random-integer packed-state range) -> x in {0..range-1}
88 ; advance the state of the generator and return the next random
89 ; range-limited integer.
90 ; Note that the state is not necessarily advanced by just one
91 ; step because we use the rejection method to avoid any problems
92 ; with distribution anomalies.
93 ; The range argument must be an exact integer in {1..m-max}.
94 ; It can be assumed that range is a fixnum if the Scheme system
95 ; has such a number representation.
96 ;
97 ; (mrg32k3a-random-real packed-state) -> x in (0,1)
98 ; advance the state of the generator and return the next random
99 ; real number between zero and one (both excluded). The type of
100 ; the result should be a flonum if possible.
101
102 ; Required: Record Data Type
103 ; ==========================
104 ;
105 ; At this point in the code, the following procedures are assumed
106 ; to be defined to create and access a new record data type:
107 ;
108 ; (:random-source-make a0 a1 a2 a3 a4 a5) -> s
109 ; constructs a new random source object s consisting of the
110 ; objects a0 .. a5 in this order.
111 ;
112 ; (:random-source? obj) -> bool
113 ; tests if a Scheme object is a :random-source.
114 ;
115 ; (:random-source-state-ref s) -> a0
116 ; (:random-source-state-set! s) -> a1
117 ; (:random-source-randomize! s) -> a2
118 ; (:random-source-pseudo-randomize! s) -> a3
119 ; (:random-source-make-integers s) -> a4
120 ; (:random-source-make-reals s) -> a5
121 ; retrieve the values in the fields of the object s.
122
123 ; Required: Current Time as an Integer
124 ; ====================================
125 ;
126 ; At this point in the code, the following procedure is assumed
127 ; to be defined to obtain a value that is likely to be different
128 ; for each invokation of the Scheme system:
129 ;
130 ; (:random-source-current-time) -> x
131 ; an integer that depends on the system clock. It is desired
132 ; that the integer changes as fast as possible.
133
134
135 ; Accessing the State
136 ; ===================
137
138 (define (mrg32k3a-state-ref packed-state)
139 (cons 'lecuyer-mrg32k3a
140 (vector->list (mrg32k3a-unpack-state packed-state))))
141
142 (define (mrg32k3a-state-set external-state)
143
144 (define (check-value x m)
145 (if (and (integer? x)
146 (exact? x)
147 (<= 0 x (- m 1)))
148 #t
149 (error "illegal value" x)))
150
151 (if (and (list? external-state)
152 (= (length external-state) 7)
153 (eq? (car external-state) 'lecuyer-mrg32k3a))
154 (let ((s (cdr external-state)))
155 (check-value (list-ref s 0) mrg32k3a-m1)
156 (check-value (list-ref s 1) mrg32k3a-m1)
157 (check-value (list-ref s 2) mrg32k3a-m1)
158 (check-value (list-ref s 3) mrg32k3a-m2)
159 (check-value (list-ref s 4) mrg32k3a-m2)
160 (check-value (list-ref s 5) mrg32k3a-m2)
161 (if (or (zero? (+ (list-ref s 0) (list-ref s 1) (list-ref s 2)))
162 (zero? (+ (list-ref s 3) (list-ref s 4) (list-ref s 5))))
163 (error "illegal degenerate state" external-state))
164 (mrg32k3a-pack-state (list->vector s)))
165 (error "malformed state" external-state)))
166
167
168 ; Pseudo-Randomization
169 ; ====================
170 ;
171 ; Reference [1] above shows how to obtain many long streams and
172 ; substream from the backbone generator.
173 ;
174 ; The idea is that the generator is a linear operation on the state.
175 ; Hence, we can express this operation as a 3x3-matrix acting on the
176 ; three most recent states. Raising the matrix to the k-th power, we
177 ; obtain the operation to advance the state by k steps at once. The
178 ; virtual streams and substreams are now simply parts of the entire
179 ; periodic sequence (which has period around 2^191).
180 ;
181 ; For the implementation it is necessary to compute with matrices in
182 ; the ring (Z/(m1*m1)*Z)^(3x3). By the Chinese-Remainder Theorem, this
183 ; is isomorphic to ((Z/m1*Z) x (Z/m2*Z))^(3x3). We represent such a pair
184 ; of matrices
185 ; [ [[x00 x01 x02],
186 ; [x10 x11 x12],
187 ; [x20 x21 x22]], mod m1
188 ; [[y00 y01 y02],
189 ; [y10 y11 y12],
190 ; [y20 y21 y22]] mod m2]
191 ; as a vector of length 18 of the integers as writen above:
192 ; #(x00 x01 x02 x10 x11 x12 x20 x21 x22
193 ; y00 y01 y02 y10 y11 y12 y20 y21 y22)
194 ;
195 ; As the implementation should only use the range {-2^53..2^53-1}, the
196 ; fundamental operation (x*y) mod m, where x, y, m are nearly 2^32,
197 ; is computed by breaking up x and y as x = x1*w + x0 and y = y1*w + y0
198 ; where w = 2^16. In this case, all operations fit the range because
199 ; w^2 mod m is a small number. If proper multiprecision integers are
200 ; available this is not necessary, but pseudo-randomize! is an expected
201 ; to be called only occasionally so we do not provide this implementation.
202
203 (define mrg32k3a-m1 4294967087) ; modulus of component 1
204 (define mrg32k3a-m2 4294944443) ; modulus of component 2
205
206 (define mrg32k3a-initial-state ; 0 3 6 9 12 15 of A^16, see below
207 '#( 1062452522
208 2961816100
209 342112271
210 2854655037
211 3321940838
212 3542344109))
213
214 (define mrg32k3a-generators #f) ; computed when needed
215
216 (define (mrg32k3a-pseudo-randomize-state i j)
217
218 (define (product A B) ; A*B in ((Z/m1*Z) x (Z/m2*Z))^(3x3)
219
220 (define w 65536) ; wordsize to split {0..2^32-1}
221 (define w-sqr1 209) ; w^2 mod m1
222 (define w-sqr2 22853) ; w^2 mod m2
223
224 (define (lc i0 i1 i2 j0 j1 j2 m w-sqr) ; linear combination
225 (let ((a0h (quotient (vector-ref A i0) w))
226 (a0l (modulo (vector-ref A i0) w))
227 (a1h (quotient (vector-ref A i1) w))
228 (a1l (modulo (vector-ref A i1) w))
229 (a2h (quotient (vector-ref A i2) w))
230 (a2l (modulo (vector-ref A i2) w))
231 (b0h (quotient (vector-ref B j0) w))
232 (b0l (modulo (vector-ref B j0) w))
233 (b1h (quotient (vector-ref B j1) w))
234 (b1l (modulo (vector-ref B j1) w))
235 (b2h (quotient (vector-ref B j2) w))
236 (b2l (modulo (vector-ref B j2) w)))
237 (modulo
238 (+ (* (+ (* a0h b0h)
239 (* a1h b1h)
240 (* a2h b2h))
241 w-sqr)
242 (* (+ (* a0h b0l)
243 (* a0l b0h)
244 (* a1h b1l)
245 (* a1l b1h)
246 (* a2h b2l)
247 (* a2l b2h))
248 w)
249 (* a0l b0l)
250 (* a1l b1l)
251 (* a2l b2l))
252 m)))
253
254 (vector
255 (lc 0 1 2 0 3 6 mrg32k3a-m1 w-sqr1) ; (A*B)_00 mod m1
256 (lc 0 1 2 1 4 7 mrg32k3a-m1 w-sqr1) ; (A*B)_01
257 (lc 0 1 2 2 5 8 mrg32k3a-m1 w-sqr1)
258 (lc 3 4 5 0 3 6 mrg32k3a-m1 w-sqr1) ; (A*B)_10
259 (lc 3 4 5 1 4 7 mrg32k3a-m1 w-sqr1)
260 (lc 3 4 5 2 5 8 mrg32k3a-m1 w-sqr1)
261 (lc 6 7 8 0 3 6 mrg32k3a-m1 w-sqr1)
262 (lc 6 7 8 1 4 7 mrg32k3a-m1 w-sqr1)
263 (lc 6 7 8 2 5 8 mrg32k3a-m1 w-sqr1)
264 (lc 9 10 11 9 12 15 mrg32k3a-m2 w-sqr2) ; (A*B)_00 mod m2
265 (lc 9 10 11 10 13 16 mrg32k3a-m2 w-sqr2)
266 (lc 9 10 11 11 14 17 mrg32k3a-m2 w-sqr2)
267 (lc 12 13 14 9 12 15 mrg32k3a-m2 w-sqr2)
268 (lc 12 13 14 10 13 16 mrg32k3a-m2 w-sqr2)
269 (lc 12 13 14 11 14 17 mrg32k3a-m2 w-sqr2)
270 (lc 15 16 17 9 12 15 mrg32k3a-m2 w-sqr2)
271 (lc 15 16 17 10 13 16 mrg32k3a-m2 w-sqr2)
272 (lc 15 16 17 11 14 17 mrg32k3a-m2 w-sqr2)))
273
274 (define (power A e) ; A^e
275 (cond
276 ((zero? e)
277 '#(1 0 0 0 1 0 0 0 1 1 0 0 0 1 0 0 0 1))
278 ((= e 1)
279 A)
280 ((even? e)
281 (power (product A A) (quotient e 2)))
282 (else
283 (product (power A (- e 1)) A))))
284
285 (define (power-power A b) ; A^(2^b)
286 (if (zero? b)
287 A
288 (power-power (product A A) (- b 1))))
289
290 (define A ; the MRG32k3a recursion
291 '#( 0 1403580 4294156359
292 1 0 0
293 0 1 0
294 527612 0 4293573854
295 1 0 0
296 0 1 0))
297
298 ; check arguments
299 (if (not (and (integer? i)
300 (exact? i)
301 (integer? j)
302 (exact? j)))
303 (error "i j must be exact integer" i j))
304
305 ; precompute A^(2^127) and A^(2^76) only once
306
307 (if (not mrg32k3a-generators)
308 (set! mrg32k3a-generators
309 (list (power-power A 127)
310 (power-power A 76)
311 (power A 16))))
312
313 ; compute M = A^(16 + i*2^127 + j*2^76)
314 (let ((M (product
315 (list-ref mrg32k3a-generators 2)
316 (product
317 (power (list-ref mrg32k3a-generators 0)
318 (modulo i (expt 2 28)))
319 (power (list-ref mrg32k3a-generators 1)
320 (modulo j (expt 2 28)))))))
321 (mrg32k3a-pack-state
322 (vector
323 (vector-ref M 0)
324 (vector-ref M 3)
325 (vector-ref M 6)
326 (vector-ref M 9)
327 (vector-ref M 12)
328 (vector-ref M 15)))))
329
330 ; True Randomization
331 ; ==================
332 ;
333 ; The value obtained from the system time is feed into a very
334 ; simple pseudo random number generator. This in turn is used
335 ; to obtain numbers to randomize the state of the MRG32k3a
336 ; generator, avoiding period degeneration.
337
338 (define (mrg32k3a-randomize-state state)
339 ;; G. Marsaglia's simple 16-bit generator with carry
340 (let* ((m 65536)
341 (x (modulo (:random-source-current-time) m)))
342 (define (random-m)
343 (let ((y (modulo x m)))
344 (set! x (+ (* 30903 y) (quotient x m)))
345 y))
346 (define (random n) ; m < n < m^2
347 (modulo (+ (* (random-m) m) (random-m)) n))
348
349 ; modify the state
350 (let ((m1 mrg32k3a-m1)
351 (m2 mrg32k3a-m2)
352 (s (mrg32k3a-unpack-state state)))
353 (mrg32k3a-pack-state
354 (vector
355 (+ 1 (modulo (+ (vector-ref s 0) (random (- m1 1))) (- m1 1)))
356 (modulo (+ (vector-ref s 1) (random m1)) m1)
357 (modulo (+ (vector-ref s 2) (random m1)) m1)
358 (+ 1 (modulo (+ (vector-ref s 3) (random (- m2 1))) (- m2 1)))
359 (modulo (+ (vector-ref s 4) (random m2)) m2)
360 (modulo (+ (vector-ref s 5) (random m2)) m2))))))
361
362
363 ; Large Integers
364 ; ==============
365 ;
366 ; To produce large integer random deviates, for n > m-max, we first
367 ; construct large random numbers in the range {0..m-max^k-1} for some
368 ; k such that m-max^k >= n and then use the rejection method to choose
369 ; uniformly from the range {0..n-1}.
370
371 (define mrg32k3a-m-max
372 (mrg32k3a-random-range))
373
374 (define (mrg32k3a-random-power state k) ; n = m-max^k, k >= 1
375 (if (= k 1)
376 (mrg32k3a-random-integer state mrg32k3a-m-max)
377 (+ (* (mrg32k3a-random-power state (- k 1)) mrg32k3a-m-max)
378 (mrg32k3a-random-integer state mrg32k3a-m-max))))
379
380 (define (mrg32k3a-random-large state n) ; n > m-max
381 (do ((k 2 (+ k 1))
382 (mk (* mrg32k3a-m-max mrg32k3a-m-max) (* mk mrg32k3a-m-max)))
383 ((>= mk n)
384 (let* ((mk-by-n (quotient mk n))
385 (a (* mk-by-n n)))
386 (do ((x (mrg32k3a-random-power state k)
387 (mrg32k3a-random-power state k)))
388 ((< x a) (quotient x mk-by-n)))))))
389
390
391 ; Multiple Precision Reals
392 ; ========================
393 ;
394 ; To produce multiple precision reals we produce a large integer value
395 ; and convert it into a real value. This value is then normalized.
396 ; The precision goal is unit <= 1/(m^k + 1), or 1/unit - 1 <= m^k.
397 ; If you know more about the floating point number types of the
398 ; Scheme system, this can be improved.
399
400 (define (mrg32k3a-random-real-mp state unit)
401 (do ((k 1 (+ k 1))
402 (u (- (/ 1 unit) 1) (/ u mrg32k3a-m1)))
403 ((<= u 1)
404 (/ (exact->inexact (+ (mrg32k3a-random-power state k) 1))
405 (exact->inexact (+ (expt mrg32k3a-m-max k) 1))))))
406
407
408 ; Provide the Interface as Specified in the SRFI
409 ; ==============================================
410 ;
411 ; An object of type random-source is a record containing the procedures
412 ; as components. The actual state of the generator is stored in the
413 ; binding-time environment of make-random-source.
414
415 (define (make-random-source)
416 (let ((state (mrg32k3a-pack-state ; make a new copy
417 (list->vector (vector->list mrg32k3a-initial-state)))))
418 (:random-source-make
419 (lambda ()
420 (mrg32k3a-state-ref state))
421 (lambda (new-state)
422 (set! state (mrg32k3a-state-set new-state)))
423 (lambda ()
424 (set! state (mrg32k3a-randomize-state state)))
425 (lambda (i j)
426 (set! state (mrg32k3a-pseudo-randomize-state i j)))
427 (lambda ()
428 (lambda (n)
429 (cond
430 ((not (and (integer? n) (exact? n) (positive? n)))
431 (error "range must be exact positive integer" n))
432 ((<= n mrg32k3a-m-max)
433 (mrg32k3a-random-integer state n))
434 (else
435 (mrg32k3a-random-large state n)))))
436 (lambda args
437 (cond
438 ((null? args)
439 (lambda ()
440 (mrg32k3a-random-real state)))
441 ((null? (cdr args))
442 (let ((unit (car args)))
443 (cond
444 ((not (and (real? unit) (< 0 unit 1)))
445 (error "unit must be real in (0,1)" unit))
446 ((<= (- (/ 1 unit) 1) mrg32k3a-m1)
447 (lambda ()
448 (mrg32k3a-random-real state)))
449 (else
450 (lambda ()
451 (mrg32k3a-random-real-mp state unit))))))
452 (else
453 (error "illegal arguments" args)))))))
454
455 (define random-source?
456 :random-source?)
457
458 (define (random-source-state-ref s)
459 ((:random-source-state-ref s)))
460
461 (define (random-source-state-set! s state)
462 ((:random-source-state-set! s) state))
463
464 (define (random-source-randomize! s)
465 ((:random-source-randomize! s)))
466
467 (define (random-source-pseudo-randomize! s i j)
468 ((:random-source-pseudo-randomize! s) i j))
469
470 ; ---
471
472 (define (random-source-make-integers s)
473 ((:random-source-make-integers s)))
474
475 (define (random-source-make-reals s . unit)
476 (apply (:random-source-make-reals s) unit))
477
478 ; ---
479
480 (define default-random-source
481 (make-random-source))
482
483 (define random-integer
484 (random-source-make-integers default-random-source))
485
486 (define random-real
487 (random-source-make-reals default-random-source))