Code Review
Compare your solutions
#| BEGIN (Write your solution here) |#
(define (random-in-range low high)
(let ((range (- high low)))
(+ low (random range))))
(define (monte-carlo trials experiment)
(define (iter trials-remaining trials-passed)
(cond ((= trials-remaining 0)
(/ trials-passed trials))
((experiment)
(iter (- trials-remaining 1) (+ trials-passed 1)))
(else
(iter (- trials-remaining 1) trials-passed))))
(iter trials 0))
(define (circle-test)
(let ((x (random-in-range -1.0 1.0))
(y (random-in-range -1.0 1.0)))
(<= (+ (* x x) (* y y)) 1.0)))
(define (estimate-integral trials)
(* 4.0 (monte-carlo trials circle-test)))
#| END |#
#| BEGIN (Write your solution here) |#
(define (square x) (* x x))
(define (in-circle? x y x0 y0 r)
(<= (+ (square (- x x0)) (square (- y y0)))
(square r)))
(define (estimate-integral predicate x1 x2 y1 y2 trials)
(define (test)
(let ((x (random-in-range x1 x2))
(y (random-in-range y1 y2)))
(predicate x y 0.0 0.0 1.0)))
(* 4.0 (monte-carlo trials test)))
#| END |#
#| BEGIN (Write your solution here) |#
(define (in-circle? x y)
(<= (+ (square x) (square y)) 1.0))
(define (estimate-integral predicate x1 x2 y1 y2 trials)
(define (test)
(let ((x (random-in-range x1 x2))
(y (random-in-range y1 y2)))
(predicate x y)))
(* 4.0 (monte-carlo trials test)))
#| END |#
#| BEGIN (Write your solution here) |#
(define (random-in-range low high)
(let ((range (- high low)))
(+ low (random range))))
(define (monte-carlo trials experiment)
(define (iter trials-remaining trials-passed)
(cond ((= trials-remaining 0)
(/ trials-passed trials))
((experiment)
(iter (- trials-remaining 1) (+ trials-passed 1)))
(else
(iter (- trials-remaining 1) trials-passed))))
(iter trials 0))
(define (circle-test)
(let ((x (random-in-range -1.0 1.0))
(y (random-in-range -1.0 1.0)))
(<= (+ (* x x) (* y y)) 1.0)))
(define (estimate-integral trials)
(* 4.0 (monte-carlo trials circle-test)))
#| END |#
#| BEGIN (Write your solution here) |#
(define (square x) (* x x))
(define (in-circle? x y x0 y0 r)
(<= (+ (square (- x x0)) (square (- y y0)))
(square r)))
(define (estimate-integral predicate x1 x2 y1 y2 trials)
(define (test)
(let ((x (random-in-range x1 x2))
(y (random-in-range y1 y2)))
(predicate x y 0.0 0.0 1.0)))
(* 4.0 (monte-carlo trials test)))
#| END |#
#| BEGIN (Write your solution here) |#
(define (in-circle? x y)
(<= (+ (square x) (square y)) 1.0))
(define (estimate-integral predicate x1 x2 y1 y2 trials)
(define (test)
(let ((x (random-in-range x1 x2))
(y (random-in-range y1 y2)))
(predicate x y)))
(* 4.0 (monte-carlo trials test)))
#| END |#