srfi-27.scm 20 KB

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