Functional Problem Solving (CSC 151 2014F) : 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.

Preparation

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

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

Exercises

Exercise 1: 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 find out how many sevens or elevens come up in n rolls? Add 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 version of tally-seven-eleven.

Exercise 2: 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 3: 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 things.

c. As you may have noted, many of the brushes don't pay attention to the current colors. However, the brushes whose names begin with "2." seem to change color appropriately. Update splat! so that it only uses brushes whose name includes "2.".

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

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.

f. As you may recall, the brushes whose names begin with "2." allow you to change their size using context-set-brush-size!. Extend splat! so that, after randomly picking one of those brushes, it also picks a random brush size.

Exercise 4: 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 image),
  • 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) and, if appropriate, a random brush size,
  • 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

Notes on Exercise 1: 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 exercise.