Fundamentals of CS I (CS151 2002F)

Randomness and Simulation

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

Exercises

Exercise 0: Preparation

a. Make a copy of dicegame.ss

b. Make the corresponding CGI script.

#!/bin/bash
/home/rebelsky/bin/schemeweb dicegame.ss

c. Share the CGI script and make it executable.

% share dicegame.ss
% 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))))
      (make-page 
        (head "A Dice Game")
        (body 
          (string-append
            (heading 1 (if (odd? (pair-a-dice)) "You Win!" "I Win!"))
            (string #\newline)
            (paragraph 
              (string-append "The roll was " (number->string (pair-a-dice))))
            (string #\newline)
            (paragraph 
              (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 dicegame.ss so that the player wins if the two dice total 7 or 11 rather than an odd number.

Exercise 4: Or Doubles

Update dicegame.ss 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 dicegame.ss so that the player can choose the amount to bet.

 

History

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 http://www.cs.grinnell.edu/~rebelsky/Courses/CS151/2002F/Labs/random.html.

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

Samuel A. Rebelsky, rebelsky@grinnell.edu