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.

## Preliminaries

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 `http://www.cs.grinnell.edu/~walker/courses/153.sp98/project-racquetball.html`.

### Racquetball

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

• The score starts at 0 - 0.
• Player A starts serving.
• When Player A wins a volley, she scores a point and is allowed to serve again.
• When Player A loses a volley, she loses the serve but no points are scored.
• Player B starts serving.
• When Player B wins a volley, she scores a point and is allowed to serve again.
• When Player B loses a volley, she loses the serve but no points are scored.

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

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.

## Preparation

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.

## Exercises

### Exercise 1: Simulating a Single Game of Racquetball

Create a PHP application that

• asks for one input, the probability that A wins a volley;
• simulates a game of racquetball; and
• prints which player wins, and whether or not the win was by a shutout.

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

• asks for two inputs: the probability that A wins a volley and the number of games to play;
• simulates the given number of games, under the assumption that A serves in the first game and that the serve alternates between games; and
• prints the summary of results (wins for A, shutouts for A, wins for B, shutouts for B).

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

### Exercise 4: Simulating Multiple Games of Volleyball

For the game of volleyball, Write a PHP application that

• asks for two inputs: the probability that A wins a volley and the number of games to play;
• simulates the given number of games, under the assumption that A serves in the first game and that the serve alternates between games; and
• prints the summary of results (wins for A, shutouts for A, wins for B, shutouts for B).

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

• asks for three inputs: the game to simulate (racquetball or volleyball), the probability that A wins a volley, and the number of games to simulate;
• simulates the given number of games, under the assumption that A serves in the first game and that the serve alternates between games; and
• prints the summary of results (wins for A, shutouts for A, wins for B, shutouts for B).

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

## History

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 `http://www.cs.grinnell.edu/~rebelsky/Courses/CSC325/2010F/Labs/simulation-lab.html`.
A PDF version of this document may be found at `http://www.cs.grinnell.edu/~rebelsky/Courses/CSC325/2010F/Labs/php_control_lab.pdf`

You may wish to validate this document's HTML ; ;

Samuel A. Rebelsky, rebelsky@grinnell.edu