灯下 登录
计算机科学 / SICP / 3.1.2 The Benefits of Introducing Assignment

Exercise 3.6 · 习题

Exercise 3.6: It is useful to be able to reset a

random-number generator to produce a sequence starting from a given value.

Design a new rand procedure that is called with an argument that is

either the symbol generate or the symbol reset and behaves as

follows: (rand 'generate) produces a new random number; ((rand

'reset) ⟨new-value⟩) resets the internal state variable to the

designated ⟨new-value⟩. Thus, by resetting the state, one can generate

repeatable sequences. These are very handy to have when testing and debugging

programs that use random numbers.

练习 3.6:能够将随机数生成器重置为从给定值开始产生序列是很有用的。设计一个新的 rand 过程,它以符号 generate 或符号 reset 之一为参数,行为如下:(rand 'generate) 产生一个新的随机数;((rand 'reset) ⟨new-value⟩) 将内部状态变量重置为指定的 ⟨new-value⟩。这样,通过重置状态,就可以生成可重复的序列。在测试和调试使用随机数的程序时,这非常方便。