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, ..., 5 |
(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 ) )
This document is available on the World Wide Web as
http://www.math.grin.edu/~walker/courses/151.fa98/lab-random.html