CSC325 2010F Web and Databases

Warning! The Web site for this class is in beta form.

Laboratory: Simple Simulation

Summary: In this laboratory, you will explore the use of PHP to provide some simple simulations.

Prerequisites: Basic PHP.


As you may know, Grinnell's Statistics faculty encourage their students to ground their learning of probability and statistics through hands-on experimentation. Recently, students in computer science have been helping these faculty build Web-based applications to support such learning.

In this laboratory, we will build some applications that could potentially help students explore the impact of small probability differences. (Along the way, we will explore what writing these applications tells us about program design.)

Let's start by exploring the rules to two similar games. (These rules are taken from Henry Walker's description at


Racquetball is a game played by two players on an indoor, enclosed court. Scoring proceeds as follows:

A player can only score points while she has the serve. A player loses the serve when she loses a volley, but no points are scored on the change of serve. Play continues until either the score is 11-0, which is a shut-out, or one player scores 21 points. (The rules do not require a player to win by two points.)


Volleyball is a game played on a court by two teams, separated by a net. Scoring proceeds much the same way as in racquetball (as explained above). In particular, scoring starts at 0-0. A team can only score points while it serves. A team loses the serve when it loses a volley, but no points are scored on the change of serve. Play continues until one team scores 15 points, and a team must win by at least two points (if the score is 15-14, play must continue until one team leads by 2 points). There is no special rule for ending a game due to a shut-out.


a. Log in to your MathLAN workstation. (Of course, you've probably already done that if you're reading this laboratory.)

b. Open a terminal window into which you can type commands.

c. Create a directory for this laboratory (e.g., public_html/Courses/CSC325/PHPControl/).

d. Open a Web browser so that you can explore the pages that you create.


Exercise 1: Simulating a Single Game of Racquetball

Create a PHP application that

Note that this is a very simple model. It assumes that the probability that A wins a volley is independent of whether or not she serves. For now, we'll live with the simple model.

Exercise 2: Simulating Multiple Games of Racqueball

Often, our statistics faculty would have students use the application you created in the previous exercise as follows: Students would run the application some number of times and count each of the events. They would then summarize the probability of A winning a game based on the probability of winning a volley.

Of course, it makes sense to have the computer simulate multiple games, rather than to have the user keep track by hand.

Write a PHP application that

Exercise 3: Detour: Exploring the Simulation

You, too, can pretend to be a statistics student.

a. If A wins 55% of the volleys, about how many games do you expect A to win out of 1000 games?

b. Check your answer experimentally.

Exercise 4: Simulating Multiple Games of Volleyball

For the game of volleyball, Write a PHP application that

Yes, I know it makes no sense to print the number of shutouts, given that shutouts in volleyball are simply 15-0, but we're striving for consistency.

Exercise 5: Simulating Either Game

Write a PHP application that

For Those With Extra Time

Many folks, upon reading the descriptions above, would find themselves dismayed at the simplicity of the simulation. In particular, they might worry that the likelihood of A winning a volley depends on whether A or B serves. (It may be, for example, that A wins 55% of the volleys in which she serves, but only 50% of the volleys in which B serves.)

Add inputs to make the simulation more realistic. You might, for example, take as inputs the probablities that (a) B successfully returns one of A's serves, (b) B successfully returns one of A's shots; (c) A successfully returns one of B's serves; and (d) A successfully returns one of B's shots.



Sunday, 5 September 2010 [Samuel A. Rebelsky]

  • Created.
  • Based in part on a legendary assignment by Henry M. Walker.


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 29 09:06:20 2010.
The source to the document was last modified on Sun Sep 5 19:28:00 2010.
This document may be found at
A PDF version of this document may be found at

You may wish to validate this document's HTML ; Valid CSS! ; Creative Commons License

Samuel A. Rebelsky,

Copyright © 2010 Samuel A. Rebelsky. 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.