27.mrg32k3a-a.upstream.scm 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. ; 54-BIT INTEGER IMPLEMENTATION OF THE "MRG32K3A"-GENERATOR
  2. ; =========================================================
  3. ;
  4. ; Sebastian.Egner@philips.com, Mar-2002.
  5. ;
  6. ; This file is an implementation of Pierre L'Ecuyer's MRG32k3a
  7. ; pseudo random number generator. Please refer to 'mrg32k3a.scm'
  8. ; for more information.
  9. ;
  10. ; compliance:
  11. ; Scheme R5RS with integers covering at least {-2^53..2^53-1}.
  12. ;
  13. ; history of this file:
  14. ; SE, 18-Mar-2002: initial version
  15. ; SE, 22-Mar-2002: comments adjusted, range added
  16. ; SE, 25-Mar-2002: pack/unpack just return their argument
  17. ; the actual generator
  18. (define (mrg32k3a-random-m1 state)
  19. (let ((x11 (vector-ref state 0))
  20. (x12 (vector-ref state 1))
  21. (x13 (vector-ref state 2))
  22. (x21 (vector-ref state 3))
  23. (x22 (vector-ref state 4))
  24. (x23 (vector-ref state 5)))
  25. (let ((x10 (modulo (- (* 1403580 x12) (* 810728 x13)) 4294967087))
  26. (x20 (modulo (- (* 527612 x21) (* 1370589 x23)) 4294944443)))
  27. (vector-set! state 0 x10)
  28. (vector-set! state 1 x11)
  29. (vector-set! state 2 x12)
  30. (vector-set! state 3 x20)
  31. (vector-set! state 4 x21)
  32. (vector-set! state 5 x22)
  33. (modulo (- x10 x20) 4294967087))))
  34. ; interface to the generic parts of the generator
  35. (define (mrg32k3a-pack-state unpacked-state)
  36. unpacked-state)
  37. (define (mrg32k3a-unpack-state state)
  38. state)
  39. (define (mrg32k3a-random-range) ; m1
  40. 4294967087)
  41. (define (mrg32k3a-random-integer state range) ; rejection method
  42. (let* ((q (quotient 4294967087 range))
  43. (qn (* q range)))
  44. (do ((x (mrg32k3a-random-m1 state) (mrg32k3a-random-m1 state)))
  45. ((< x qn) (quotient x q)))))
  46. (define (mrg32k3a-random-real state) ; normalization is 1/(m1+1)
  47. (* 0.0000000002328306549295728 (+ 1.0 (mrg32k3a-random-m1 state))))