Introduction: Many computing applications involve the simulation of games or events, with the hope of gaining insights and identifying underlying principles. In some cases, simulations can apply definite, well-known formulae. For example, in studying the effect of a pollution source in a lake or stream, one can keep track of pollutant concentrations in various places. Then, since the flow of water and the interactions of pollutants is reasonably well understood, one can follow the flow of the pollutants over a period time, according to known equations.
In other cases, specific outcomes involve some chance. For example, when a car begins a trip and encounters a traffic light, it may be a matter of chance as to whether the light is green or not. Similar uncertainties arise when considering specific organisms or when tabulating the outcomes involving flipping a coin, tossing a die, or dealing cards. In these cases, one may know about the probability of an event occuring (a head may occur about half the time), but the result of any one event depends on chance.
In studying events that involve some chance, one approach is to model the event or game, using a random number generator as the basis for decisions. If such models are run on computers many times, the results may give some statistical information about what outcomes are likely and how often each type of outcome might be expected to occur. This approach to problem solving is called the Monte Carlo Method.
The Random Procedure: A random number generator for a typical computer language is a procedure which produces different values each time it is called. Such procedures simulate a random selection process. Chez Scheme contains the procedure random for this purpose. This procedure returns either integer or real values, depending upon how it is called. Here are several examples.
| Sample Procedure Call | Type of Value Returned |
|---|---|
| (random 10) | random integer 0, 1, ..., 9 |
| (random 6) | random integer 0, 1, ..., 6 |
| (random 1.0) | random nonnegative real number less than 1.0 |
(define roll-a-die
(lambda () ;;; (random 6) gives integers 0,1,2,3,4,5
(+ 1 (random 6)) ;;; so add 1 to get integers 1,2,3,4,5,6
)
)
(define roll
(lambda (n)
(if (<= n 0)
'() ;;; rolling a die 0 times gives null list
(cons (roll-a-die) (roll (- n 1))))
;;; add current roll to list of rest
)
)
(define pair-of-dice
(lambda ()
(list (roll-a-die) (roll-a-die))
)
)
(define seven-or-11
(lambda (n)
(cond ((<= n 0) 0)
((or (= (sum-of-dice) 7) (= (sum-of-dice) 11))
(+ 1 (seven-or-11 (- n 1))))
(else (seven-or-11 (- n 1)))
)
)
)
Challenge Problem 2:
Write a procedure double-heads which tosses a coin n
times and determines whether a head ever comes up twice in a row.
(Hint: You might try adding another parameter which indicates if the
previous toss were a head.)
Challenge Problem 3: Modify the previous procedure to record the number of times a double head is obtained when a coin is tossed n times. (In your counting, you should consider three heads in a row as two double heads.)
Challenge Problem 4: Modify your procedure from Challenge Problem 2, so that it continues the simulation until a double head appears and then prints the number of flips that were required to get the double head.
Extra Credit Opportunity:
This document is available on the World Wide Web as
http://www.math.grin.edu/~walker/courses/153/lab-simple-simulations.html