Fundamentals of Computer Science I: Media Computing (CS151.02 2007F)

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.

Preparation

a. Start DrFu, create a new 200x200 image, and call it canvas.

b. Copy the primary random drawing definitions from the reading to your library. You can find those definitions at the end of this lab.

c. Put an instruction to load your library into the start of your definitions window.

Exercises

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? Check your hypothesis experimentally.

c. What do you expect to happen if you call random with 0 or -1 as a parameter? Check your hypothesis experimentally.

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

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

Exercise 2: Rolling Dice

a. Copy the roll-a-die and roll-dice procedures from the reading. You can also find them at the end of this lab.

b. Using roll-dice, roll ten dice.

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

d. Did you get the same list of values each time?

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

(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? Use the analysis tools (define$ and analyze) to find out how many times it is actually called. 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?

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

Here is the splat! from the reading.

;;; Procedure:
;;;   splat!
;;; Parameters:
;;;   image, an image
;;; Purpose:
;;;   Draw a line between random points, using a random color and
;;;   a random brush.
;;; Produces:
;;;   (nothing)
;;; Postconditions:
;;;   The foreground color may have changed.
;;;   The brush may have changed.
;;;   The image now contains another line.
(define splat!
  (lambda (image)
    (envt.set-fgcolor! (random-color))
    (select-random-brush!)
    (draw-random-line! image)
    (update-displays)))

Add it to your definitions pane.

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

b. Call splat! a few times to draw a few more 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.

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)
> (stroke 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 mincol maxcol minrow maxrow), that draws a blob which is restricted to be between horizontally between mincol and maxcol and vertically between minrow and maxrow.

Notes

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.

Useful Procedures

Rolling Dice

;;; Procedure:
;;;   roll-a-die
;;; Parameters:
;;;   None
;;; Purpose:
;;;   To simulate the rolling of one six-sided die.
;;; Produces:
;;;   An integer between 1 and 6, inclusive.
;;; Preconditions:
;;;   [None]
;;; Postconditions:
;;;   Returns an integer between 1 and 6, inclusive.
;;;   It should be difficult (or impossible) to predict which
;;;     number is produced.
(define roll-a-die
  (lambda ()
    (+
      (random 6)    ; a value in the range [0 .. 5]
      1)))          ; now in the range [1 .. 6]

(define roll-dice
  (lambda ()
    (+ (roll-a-die) (roll-a-die))))

Random Drawing

;;; Value:
;;;   colors.rainbow
;;; Type:
;;;   List of colors 
;;; Contains:
;;;   A list of colors of the rainbow, suitable for processing by 
;;;   random-rainbow-color.
(define colors.rainbow
  (list color.red color.orange color.yellow color.green color.blue
        color.indigo color.violet))

;;; Procedure:
;;;   random-rainbow-color
;;; Parameters:
;;;   [None]
;;; Purpose:
;;;   Picks a "random" (unpredictable) color from the basic colors
;;;   of the rainbow.
;;; Produces:
;;;   color, a string
;;; Preconditions:
;;;   Value colors.rainbow has been defined as a list of colors.
;;; Postconditions:
;;;   color is a color.
;;;   It is difficult for someone to predict what color random-color
;;;     will return.
(define random-rainbow-color
  (lambda ()
    (list-ref colors.rainbow
              (random (length colors.rainbow)))))

;;; Procedure:
;;;   random-color
;;; Parameters:
;;;   (none)
;;; Purpose:
;;;   Selects and returns a random color.
;;; Produces:
;;;   color, a color.
;;; Postconditions:
;;;   It is difficult to predict color.
(define random-color
  (lambda ()
    (rgb.new (random 256) (random 256) (random 256))))

;;; Procedure:
;;;   list.random-element
;;; Parameters:
;;;   values, a list
;;; Purpose:
;;;   Randomly select an element of values.
;;; Produces:
;;;   value, a value
;;; Preconditions:
;;;   values is nonempty.
;;; Postconditions:
;;;   value is an element of values.
;;;   value is equally likely to be any element of values.
(define list.random-element
  (lambda (values)
    (list-ref values (random (length values)))))

;;; Procedure:
;;;   randomly-select-brush!
;;; Parameters:
;;;   brushes, a list of strings
;;; Purpose:
;;;   Select one of brushes.
;;; Produces:
;;;   (nothing)
;;; Preconditions:
;;;   All the strings in brushes name valid brushes.
;;; Postconditions:
;;;   The current brush is an element of brushes.
;;;   It is equally likely that each element of brushes is now the
;;;     active brush
 (define randomly-select-brush!
  (lambda (brushes)
    (envt.set-brush! (list.random-element brushes))))

;;; Procedure
;;;   select-random-brush!
;;; Parameters:
;;;   (none)
;;; Purpose:
;;;   Select one of the brushes.
;;; Produces:
;;;   (nothing)
;;; Postconditions:
;;;   It is difficult to predict the brush.
 (define select-random-brush!
  (lambda ()
    (randomly-select-brush! (list-brushes))))

;;; Value:
;;;   colors.blues
;;; Type: 
;;;   List of colors.
;;; Value:
;;;   All the colors whose name includes the word "blue".
(define colors.blues
  (map cname->rgb (cname.list "blue")))


;;; Procedure: 
;;;   random-blue
;;; Parameters:
;;;   (none)
;;; Purpose:
;;;   Selects a color with blue in its name.
;;; Produces:
;;;   some-blue, a color
;;; Preconditions:
;;;   blue-colors is defined.
;;; Postconditions:
;;;   some-blue is a color whose name includes "blue".
;;;   It is difficult to predict which blue it is.
(define random-blue
  (lambda ()
    (list.random-element blue-colors)))

;;; Procedure:
;;;   draw-random-line!
;;; Parameters:
;;;   image, an image
;;; Purpose:
;;;   Draw a random line in the image, assuming that its width
;;;   and height are as specified.
;;; Produces:
;;;   (nothing)
;;; Postconditions:
;;;   A new line has been added to image, using the current color
;;;   and brush.
(define draw-random-line!
  (lambda (image)
    (line image (random (image.width image)) (random (image.height image))
                (random (image.width image)) (random (image.height image)))))

Creative Commons License

Samuel A. Rebelsky, rebelsky@grinnell.edu

Copyright 2007 Janet Davis, Matthew Kluber, and Samuel A. Rebelsky. (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 http://creativecommons.org/licenses/by-nc/2.5/ or send a letter to Creative Commons, 543 Howard Street, 5th Floor, San Francisco, California, 94105, USA.