练习3-81
原文
Exercise 3.81. Exercise 3.6 discussed generalizing the random-number generator to allow one to reset the random-number sequence so as to produce repeatable sequences of “random” numbers. Produce a stream formulation of this same generator that operates on an input stream of requests to generate a new random number or to reset the sequence to a specified value and that produces the desired stream of random numbers. Don’t use assignment in your solution.
代码
(define (random-update x) (remainder (+ (* 13 x) 5) 24))
;Value: random-update
(define random-init (random-update (expt 2 32)))
;Value: random-init
(define (random-number-generator command-stream) (define random-number (cons-stream random-init (stream-map (lambda (number command) (cond ((null? command) the-empty-stream) ((eq? command 'generator) (random-update number)) ((and (pair? command) (eq? (car command) 'reset)) (cdr command)) (else (error "bad command -- " command)))) random-number command-stream))) random-number)
;Value: random-number-generator
感谢您的访问,希望对您有所帮助。 欢迎大家关注或收藏、评论或点赞。
为使本文得到斧正和提问,转载请注明出处:
http://blog.youkuaiyun.com/nomasp
版权声明:本文为 NoMasp柯于旺 原创文章,未经许可严禁转载!欢迎访问我的博客:http://blog.youkuaiyun.com/nomasp
随机数生成器流式实现
本文介绍了一种不使用赋值操作的随机数生成器流式实现方法,该生成器能根据输入请求流生成新的随机数或将序列重置到指定值,确保了随机数序列的可重复性。
312

被折叠的 条评论
为什么被折叠?



