Fundamentals of CS I (CS151 2002F)

Randomness and Simulation

Summary: In this laboratory, you will explore that random procedure using some fun Web services.


Exercise 0: Preparation

a. Make a copy of

b. Make the corresponding CGI script.


c. Share the CGI script and make it executable.

% share
% share dicegame.cgi
% chmod a+x dicegame.cgi

d. Start DrScheme

Exercise 1: Experimentation

Verify that the dicegame.cgi script works.

Exercise 2: Improving Code

Sarah and Steven Schemer are somewhat stymied by the use of a helper procedure for game-page. They'd like to replace that pair of procedures with

(define game-page
  (lambda (account)
    (let ((new-account (+ account (if (odd? (pair-a-dice)) 1 -1))))
        (head "A Dice Game")
            (heading 1 (if (odd? (pair-a-dice)) "You Win!" "I Win!"))
            (string #\newline)
              (string-append "The roll was " (number->string (pair-a-dice))))
            (string #\newline)
              (string-append "You now have $" 
                (number->string new-account)))
            (string #\newline)
            (form "dicegame.cgi" "Roll!"
              (hidden "account"
                (number->string new-account)))
          ) ; string-append
        ) ; body
      ) ; make-page
    ) ; let
  ) ; lambda
) ; define game-page

a. What do you think about their idea?

Note that you might test the effects of this modified procedure from within DrScheme. It might be helpful to add a line to pair-a-dice that uses display to indicate that the procedure has been called.

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

Note that you should remove the display before running your script through the Web.

b. I originally wrote this problem before you learned let. Use let to rewrite game-page without a helper procedure.

Exercise 3: Seven or Eleven

Update so that the player wins if the two dice total 7 or 11 rather than an odd number.

Exercise 4: Or Doubles

Update so that the player wins if the two dice total 7 or 11 or if both dice have the same value.

Exercise 5: Changing Bets

Update so that the player can choose the amount to bet.



Thursday, 19 September 2002 [Samuel A. Rebelsky]

Friday, 20 September 2002 [Samuel A. Rebelsky]

Monday, 4 November 2002 [Samuel A. Rebelsky]


Disclaimer: I usually create these pages on the fly, which means that I rarely proofread them and they may contain bad grammar and incorrect details. It also means that I tend to update them regularly (see the history for more details). Feel free to contact me with any suggestions for changes.

This document was generated by Siteweaver on Mon Nov 4 08:29:18 2002.
The source to the document was last modified on Mon Nov 4 08:29:11 2002.
This document may be found at

You may wish to validate this document's HTML ; Valid CSS! ; Check with Bobby

Samuel A. Rebelsky,