Laboratory Exercises For Computer Science 153

Simple Simulations

Simple Simulations

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.

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
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.

  1. Type each of the examples into Scheme several times.
    (For example, you might enter (random 6) into the computer ten times.)
    Describe what results.

  2. Type (random 1). Why do you think the result always is 0?

  3. What happens if you type (random -5)?
Using a Random Number Generator: Rolling a Die. 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
      )
   )
  1. Enter the definition of roll-a-die into Scheme. Confirm that roll-a-die works correctly by typing (roll-a-die) several times.

  2. Explain why the parentheses are needed when running the roll-a-die procedure.
    (Consider, for example, what happens if roll-a-die is typed without the parentheses.)
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
      )
   )
  1. Enter the definition of roll into Scheme, and check that it works correctly.
    Explain in a few sentences why roll simulates the rolling of a die.

  2. What happens if you call (roll 10) several times?
    Do you get the same list of 10 die rolls each time?

  3. Modify your previous work to obtain a procedure count-2s, which counts the number of 2's rolled when a die is thrown n times.
Rolling a Pair of Dice: We can simulate the rolling a pair of dice by calling roll-a-die twice -- perhaps making a list out of the two results:

   (define pair-of-dice
       (lambda ()
           (list (roll-a-die) (roll-a-die))
       )
   )
  1. Check that pair-of-dice works correctly.

  2. Combine the ideas of procedures roll and pair-of-dice to obtain a procedure roll-dice that shows the results of throwing a pair of dice n times.

  3. Modify pair-of-dice to obtain a new procedure sum-of-dice that returns the sum of two dice. (As with pair-of-dice, the sum-of-dice procedure should not require any parameters.)

  4. Modify roll to obtain a procedure count-7s, which counts the number of 7's rolled when a pair of dice is thrown n times.
Tossing Coins: 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.
  1. Write a procedure coin which simulates the flipping of a coin.
    (coin should return either the string "head" or the string "tail".)

  2. Build on your coin procedure by writing a procedure tosses which simulates the flipping of a coin n times. The results of each toss should be placed on a list.

  3. Write a procedure number-of-heads that counts the number of heads that are obtained in n tosses.
Challenge Problem 1: Consider the problem of rolling a pair of dice n times and counting the number of times that either a 7 or an 11 comes up.

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

created February 5, 1997
last revised January 11, 1998