srfi-27.scm 20 KB

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