27.mrg32k3a.upstream.scm 17 KB

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