(define (random-number-generator command-stream)
(define (random init)
(define rand-stream
(cons-stream init
(stream-map rand-update rand-stream))))
(define (read-random-number stream)
(stream-car stream))
(define (random-number-generator-temp command-stream random-stream)
(let ((command (stream-car command-stream)))
(cond ((eq? command 'generate)
(begin (read-random-number stream)
(random-number-generator-temp (stream-cdr command-stream)
(stream-cdr random-stream))))
((pair? command)
(let ((action (car command))
(init (cdr command)))
(if (eq? action 'reset)
(random-number-generator-temp (stream-cdr command-stream)
(random init))
(error "Unexpected command"))))
(else
(error "Unexpected command")))))
random-number-generator-temp)