random.scm 756 B

123456789101112131415161718192021
  1. (define-module (utils random))
  2. (use-modules (srfi srfi-27))
  3. (define-public make-random-integer-generator
  4. (lambda* (#:key seed)
  5. "Get a procedure for generating random integers from 0 up to a not included
  6. bound, which is seeded by the keyword argument seed, which must be a positive
  7. integer."
  8. (cond
  9. [seed
  10. (let ([rand-src (make-random-source)])
  11. (random-source-pseudo-randomize! rand-src 0 seed)
  12. (random-source-make-integers rand-src))]
  13. [else
  14. (let ([rand-src (make-random-source)])
  15. ;; Try to make the random source truly random. How this works depends on
  16. ;; the specific implementation of SRFI-27.
  17. (random-source-randomize! rand-src)
  18. (random-source-make-integers rand-src))])))