Laboratory Exercises For Computer Science 151

Random Number Generators

Random Number Generators

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

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

This document is available on the World Wide Web as

created February 5, 1997
last revised September 14, 1998