;;; rand.scm ;;; Created by Robert R. Snapp, Copyright (C) 2004 ;;; ;;; This file contains pseudo-random number generators that use the linear congruence method: ;;; ;;; x' = (a*x + b) mod m ;;; ;; The random number generator rand is implemented using "global variables" (define a 123456789) ; a global definition (define b 9874923843) ; a global definition (define m 10000000) ; a global definition (define x 234987) ; another global definition (define (rand) (set! x (remainder (+ (* a x) b) m)) ; remainder impliments modulo division x) ; now return the new random number ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; The random number generator newrand uses "local variables" using a device known as a "closure" (define newrand '*) ; a "dummy" definition. (let ((a 249834723) ; let defines local variables (b 987438243) ; another local definition (m 100000000) (x 234984734)) (set! newrand ; We redefine newrand so that it generates random numbers using the local variables. (lambda () (set! x (remainder (+ (* a x) b) m)) x)))