random.scm 1001 B

12345678910111213141516171819202122232425262728293031
  1. (define-module (random))
  2. (use-modules
  3. ;; SRFI-27 for random number utilities
  4. (srfi srfi-27))
  5. (define-public make-random-integer-generator
  6. (lambda* (#:key seed)
  7. "Get a procedure for generating uniformly distributed
  8. random integers from 0 up to a not included bound, which is
  9. seeded by the keyword argument seed, which must be a
  10. positive integer."
  11. (cond
  12. [seed
  13. (let ([rand-src (make-random-source)])
  14. ;; Set the given seed to guarantee same results for
  15. ;; same invokations.
  16. (random-source-pseudo-randomize! rand-src 0 seed)
  17. ;; Obtain a procedure, which gives uniformly
  18. ;; distributed integers.
  19. (random-source-make-integers rand-src))]
  20. [else
  21. (let ([rand-src (make-random-source)])
  22. ;; Try to make the random source truly random. How
  23. ;; this works depends on the specific implementation
  24. ;; of SRFI-27.
  25. (random-source-randomize! rand-src)
  26. (random-source-make-integers rand-src))])))