Goals: The first part of this laboratory introduces the concept of a random number generator. The remaining parts of the lab then use such a generator to simulate various experiments involving the rolling of a die or the flipping of a coin.
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.
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 |
In general, (random x) returns a random nonnegative number less than x. If x is an integer, the result is an integer. If x is a real number, the result is real. In each case, zero is a possible result, but the value x is not.
We can use Random to write a program to simulate the rolling of a die, by generating integers from 1 to 6, to correspond to the faces on the die cube. The details of this simulation are shown in the following procedure:
(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
)
)
To roll a die n times, we could construct a list with each result of calling the roll-a-die procedure. A parameter n allows us to count the number of rolls left to go.
(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
)
)
We can simulate the tossing of a coin by generating real numbers between 0 and 1 and then considering a "head" to correspond to a number less than 0.5.
This document is available on the World Wide Web as
http://www.math.grin.edu/~walker/courses/151.sp04/readings/random-numbers.xhtml
|
created February 5, 1997 last revised January 16, 2004 |
|
| For more information, please contact Henry M. Walker at walker@cs.grinnell.edu. |