CS151, Class 25: Randomness and Simulation Overview: * The "random" procedure. * SImulating dice. * A Web game. * Lab Notes: * Readings: Randomness and Simulation, Pairs * Happy Seuss-week * Who will be in class on Friday the 14th? Class cancelled * Who went to Alex's concert? (Email me.) * Homework 2 graded. * Questions on homework 3? + Send me lists of books by tonight. * You don't have to put everything in a procedure! (define sum-of-one-two-three (lambda () (+ 1 2 3))) "Sum the last elements of the lists (5 6) (7) (8 9 10)" (letrec ((last ...)) (+ (last (list 5 6)) (last (list 7)) (last (list 8 9 10)))) * Don't use images that are not your own. * Exam 1 returned. Some discussion. (if (test1) val1 (if (test2) val2 (if (test3) val3 ... INELEGANT (-N) (cond ((test1) val1) ((test2) val2) (..)) (if (test) #t #f) is INELEGANT use (test) (if (test) #f #t) is INELEGANT use (not (test)) Choose the appropriate procedure for the data type. TEST TEST TEST! > (a-or-an "apple") "an apple" > (a-or-an "pizza") "a pizza" (if (char=? (string-ref word 0) (or #\a #\e #\i #\o #\u)) (string-append "an " word) (string-append "a " word)) if (or (char=? (string-ref word 0) #\a) (char=? (string-ref word 0) #\e) (char=? (string-ref word 0) #\i) (char=? (string-ref word 0) #\o) (char=? (string-ref word 0) #\u)) (simple? 'a) (simple? 3.4) (simple? "a") (simple? (list 1 2 3)) (simple? #t) (simple? #f) (simple? #\a) (simple? simple?) (define join (lambda (thing1 thing2) (cond ((and (simple? thing1) (simple? thing2)) (list thing1 thing2)) ((and (simple? thing1) (list? thing2)) (cons thing1 thing2)) ((and (list? thing1) (list? thing2)) (list thing1 thing2)) ((and (list? thing1) (simple? thing2)) (list thing1 (list thing2))) (else (error 'join "You are so simple you don't know what's simple"))))) > (join simple? simple?) > quadradic ---------------------------------------- The "random" procedure. (random num) returns an integer between 0 and num-1, inclusive num is an integer. It may return different numbers at different times. Why is this useful? * Make games.' * Lets us model the world. * Fun for homework 3. (define random-element (lambda (lst) (list-ref lst (random (length lst))))) (random-element (list "red" "yellow" "green")) (define roll-a-six-sided-die (lambda () (+ 1 (random 6))))