-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path3_05.rkt
40 lines (33 loc) · 1.24 KB
/
3_05.rkt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#lang racket
#| Solution for exercise 3_05. |#
(require "../solutions/utils.rkt")
(provide estimate-unit-disk-area)
(define (monte-carlo trials experiment)
(define (iter-trials trials-remaining trials-passed)
(cond ((= trials-remaining 0)
(/ trials-passed trials))
((experiment)
(iter-trials (- trials-remaining 1) (+ trials-passed 1)))
(else
(iter-trials (- trials-remaining 1) trials-passed))))
(iter-trials trials 0))
(define (estimate-unit-disk-area trials)
(estimateintegral (isInCircle 0 0 1) -1 1 -1 1 trials))
(define (estimateintegral P x1 x2 y1 y2 trials)
(define (test)
(let ((x-random (random-in-range x1 x2))
(y-random (random-in-range y1 y2)))
(P x-random y-random)))
(let ((square-area (* (- x2 x1) (- y2 y1))))
(* (monte-carlo trials test) square-area)))
(define (isInCircle x-centre y-centre radius)
(lambda (x y)
(<= (+ (square (- x x-centre))
(square (- y y-centre)))
(square radius))))
(define (random-in-range low high)
(let* ((precise 10000)
(precise-low (* low precise))
(precise-high (* high precise))
(range (- precise-high precise-low)))
(/ (+ precise-low (random range) 0.) precise)))