Chapter 3, Modularity, Objects, and State

Exercise 3.6


I could not find a way to seed in the sicp package of DrRacket. So just implement the procedure and used random <val> - and passed seed as val to this procedure for naive testing :)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#lang sicp

(#%require (only racket/base error))

(define random-seed 100)

; TODO: change it with the correct procedure to seed. This wont 
; work as expected. `random <val>` returns a random number less than the passed value - which 
; is not what we want :)
(define (rand-update val)
  (random val))

(define (rand m)
    (cond
      ((eq? 'generate m) (set! random-seed (rand-update random-seed)) random-seed)
      ((eq? 'reset m) (lambda(n) (set! random-seed n) random-seed))
      (else (error "Invalid parameter"))))