;; CMPU 145, Spring 2019 ;; Lab 7 #lang racket (include "asmt-helper.scm") (include "lab6-solns.scm") (header "Lab 7" "YOUR NAME") (problem "Introduction") ;; GEN-AND-TEST ;; ------------ ;; INPUTS: N, a non-negative integer ;; GEN, a function of one input that is used to generate random ;; instances of some event (e.g., tossing a die, dealing a card) ;; TEST, a function that tests whether some given instance has some ;; property (e.g., die > three, or card is a face card) ;; OUTPUT: The fraction of N random trials in which the randomly generated ;; instances satisfied the TEST. (define gen-and-test (lambda (n gen test [print #f]) ;; COUNT is a counter variable (let ((count 0)) ;; FOR executes its body N times (for ((i n)) ;; Use (GEN) to generate a random instance; ;; Use TEST to determine whether that instance has the ;; desired property (let* ((rnd (gen)) (result (test rnd))) (when print (printf "[~A: ~A] " rnd result)) (when result ;; If it does, then destructively increment the counter! (set! count (+ count 1))))) ;; After the FOR loop finishes, return the fraction of the ;; trials that satisfied the TEST. (/ count n 1.0)))) ;; TOSS-DIE ;; -------- ;; INPUTS: None ;; OUTPUT: A randomly generated number from 1 to 6 (define toss-die (lambda () (+ 1 (random 6)))) ;; TEST-DIE ;; -------- ;; INPUT: A number from 1 to 6 ;; OUTPUT: #t if that number is either 5 or 6 (define test-die (lambda (die) (or (= die 5) (= die 6)))) (tester '(gen-and-test 1000 toss-die test-die)) (tester '(gen-and-test 10000 toss-die test-die)) (newline) (problem "1. Pair of dice") ;; ... (problem "2. Properties of natural numbers") ;; Generate a natural number between 0 and 99, represented as a list of 1s (define random-nn (lambda () (make-nn (random 100)))) (define test-claim1 (lambda (nn) (equal? (nn-addn nn0 nn) nn))) (tester '(gen-and-test 10 random-nn test-claim1)) (tester '(gen-and-test 100 random-nn test-claim1)) ;; ...