3.5 Exercise 3.5
In the interest of sharing, I thought I’d show you that I made a rand
of my own by using the random function (which computes a random number
from 0 to its given argument) with the biggest value it will accept.
(define (rand) | (random 4294967087)) |
|
It’s not pretty, but it works, and allows you to verify that the Monte Carlo
methods actually work. Now, back to regular programming.
Defining a new Monte Carlo experiment is easy: All we need to do is define
a procedure of no parameters for our experiment and pass it, along with a
given number of trials, to monte-carlo. The experiment procedure
can use whatever parameters it wants, as long as it takes no parameters
to run (that is, you can use parameters to set up the kind of experiment).
In this case, we set up the experiment procedure with the bounds of the
rectangle we are generating points in and the predicate we want to test.
estimate-integral turns out to be a simple procedure:
(define (estimate-integral P trials x1 x2 y1 y2) | (define (test-point) | (let ((x (random-in-range x1 x2)) | (y (random-in-range y1 y2))) | (P x y))) | (monte-carlo trials test-point)) |
|
We can define the predicate in the book like this:
(define (P x y) | (>= 9 | (+ (square (- x 5)) | (square (- y 7))))) |
|