CSC151.01 2009F Functional Problem Solving : Labs

Laboratory: Randomized Drawing

Summary: In this laboratory, you will explore the random procedure, its use in simulating simple games, and its use in making “unpredictable” drawings.


a. Start the GIMP and MediaScript, create a new 200x200 image, and call it canvas.

b. Make a copy of random-drawing-lab.scm.


Exercise 1: Testing random

a. Evaluate the expression (random 10) twenty times. What values do you get?

b. What values do you expect to get if you call random with 1 as a parameter?

c. Check your hypothesis experimentally.

d. What do you expect to happen if you call random with 0 or -1 as a parameter?

e. Check your hypothesis experimentally.

f. Try calling random with other integer parameters. What effect does the parameter seem to have?

g. Try calling random with non-integer parameters. What effect does the parameter seem to have?

h. Try calling random with no parameters. What happens?

Exercise 2: Rolling Dice

a. Using roll-dice, roll ten dice.

b. Using roll-dice, roll ten dice. (Yes, this instruction is the same as the previous instruction. You should do it twice.)

c. Did you get the same list of values each time? Why or why not?

d. What other procedures have you encountered that may return different values each time you call them with the same parameters?

Exercise 3: Sevens or Elevens

Consider the problem of rolling a pair of dice n times and counting the number of times that either a seven (7) or an eleven (11) comes up.

a. What is wrong with the following pair of procedures that are intended to accomplish this task.?

;;; Procedure:
;;;   pair-a-dice
;;; Parameters:
;;;   [None]
;;; Purpose:
;;;   Roll two six-sided dice and find their sum.
;;; Produces:
;;;   roll, an integer
;;; Preconditions:
;;;   [No additional]
;;; Postconditions:
;;;   2 <= roll <= 12
;;;   Across multiple calls, the various rolls have the same probabilities 
;;;     as we would get from rolling two dice.
(define pair-a-dice
  (lambda ()
    (+ (roll-a-die) (roll-a-die))))

(define tally-seven-eleven
  (lambda (n)
     (cond ((<= n 0) 0)
           ((or (= (pair-a-dice) 7) (= (pair-a-dice) 11))
            (+ 1 (tally-seven-eleven (- n 1))))
           (else (tally-seven-eleven (- n 1))))))

Hint: How many times should we roll a pair of dice to count how many times to find out how many sevens or elevens come up in n rolls? Add a an expression using display to the pair-a-dice procedure so that you can count how many times it is called.

(define pair-a-dice
  (lambda ()
    (display "Rolling ...") (newline)
    (+ (roll-a-die) (roll-a-die))))

If that isn't enough of a hint, read the notes on this problem.

b. Write a correct procedure to solve this problem.

Exercise 4: Practice with Random Drawing Procedures

a. What do you expect the following to accomplish?

> (context-set-fgcolor! (random-rainbow-color))
> (select-random-brush!)
> (draw-random-line! canvas)

b. Check your answer experimentally.

c. Do you expect to get the same result if you enter the same sequence of instructions again? Why or why not?

d. Check your answer experimentally.

e. What do you expect to have happen if you replace random-rainbow-color in the instructions with random-color.

f. Check your results experimentally.

Exercise 5: Splats, Revisited

Your code file should include the splat! procedure from the reading.

a. Read through the code for splat! to make sure that you understand what it does.

b. Call splat! a few times to draw a few lines.

c. As you may have noted, there are two procedures that generate one of a limited range of random colors, random-rainbow-color and random-blue. Pick one of the two and update splat! to use it. Test your updated version.

d. Update splat! so that it only uses circular brushes.

e. Pick a few favorite brushes and update splat! so that it selects between those brushes.

Hint: The reading had a procedure that lets you select between a set of brushes. That procedure is included in the code file for this lab.

Exercise 6: New Random Drawings

a. Write a procedure, (select-random-ellipse! image), that selects an unpredictable eclipse in image.

b. Test your procedure with a sequence of commands like the following:

> (select-random-ellipse! canvas)
> (image-stroke-selection! canvas)

c. In addition to selecting a random ellipse, we might also want to choose a random fill color, a random stroke color, and even a random brush. Rather than retyping that sequence of commands each time we want a new random ellipse, we might encapsulate them into a procedure, which we could call draw-blob!.

Write a procedure, (draw-blob! image), that

  • selects a random ellipse (not all of which needs to be in the figure),
  • chooses a random color (perhaps from a restricted group of colors),
  • fills the ellipse,
  • chooses another random color (perhaps from a restricted list of colors),
  • chooses a random brush (perhaps from a restricted list of brushes),
  • strokes the ellipse, and
  • clears the selection.

For Those With Extra Time

Extra 1: Repeated Blobs

In an earlier exercise, you wrote a procedure, draw-blob! and called it a few times. In practice, most programmers don't like to enter the name of a procedure again and again. What's the solution? Write another procedure that repeatedly calls that procedure.

Write and experiment with a procedure, (draw-blobs! image times), that draws a blob on the image the specified number of times.

Extra 2: Restricted Blobs

One problem with draw-blob! is that the blobs can be anywhere and any size. The client of draw-blob! might want to put some limits on the procedure. Write a new procedure, (draw-restricted-blob! image min-col max-col min-row max-row), that draws a blob which is restricted to be between horizontally between min-col and max-col and vertically between min-row and max-row.


Notes on Problem 3: Sevens or Elevens

If there are n rolls to count, we should only roll the dice n times. However, you will find that tally-seven-eleven does somewhere between n and 2n calls. Why? Because the “is it seven or eleven” potentially rolls the dice twice, once to see if the roll is seven and, if not, one more time to see if the roll is eleven.

Return to the problem.

Creative Commons License

Samuel A. Rebelsky,

Copyright (c) 2007-9 Janet Davis, Matthew Kluber, Samuel A. Rebelsky, and Jerod Weinman. (Selected materials copyright by John David Stone and Henry Walker and used by permission.)

This material is based upon work partially supported by the National Science Foundation under Grant No. CCLI-0633090. Any opinions, findings, and conclusions or recommendations expressed in this material are those of the author(s) and do not necessarily reflect the views of the National Science Foundation.

This work is licensed under a Creative Commons Attribution-NonCommercial 2.5 License. To view a copy of this license, visit or send a letter to Creative Commons, 543 Howard Street, 5th Floor, San Francisco, California, 94105, USA.