random-source-state-set! doesn't restore the prng state

Create issue
Issue #32 resolved
Takashi Kato repo owner created an issue

This should not raise an error.

(import (scheme base)
        (scheme write)
        (srfi 27))

(define (my-random-integer n)
  (let ((x (random-integer n)))
    (if (<= 0 x (- n 1))
        (error "(random-integer n) returned illegal value" x))))

(display "; get/set state: ")
(let* ((state1 (random-source-state-ref default-random-source))
       (x1 (my-random-integer (expt 2 32)))
       (state2 (random-source-state-ref default-random-source))
       (x2 (my-random-integer (expt 2 32))))
  (random-source-state-set! default-random-source state1)
  (let ((y1 (my-random-integer (expt 2 32))))
    (if (not (= x1 y1))
        (error "state get/set doesn't work" x1 y1 state1)))
  (random-source-state-set! default-random-source state2)
  (let ((y2 (my-random-integer (expt 2 32))))
    (if (not (= x2 y2))
        (error "state get/set doesn't work" x2 y2 state2))))
(display "ok")

Comments (1)

  1. Log in to comment