CS
205 - Programming for the Sciences
Spring 2008 - Programming
Assignment 3
20 points
Out:
February 21, 2008
Due: February 28, 2008
In games using a pair of dice, it is important to know the probability of rolling a particular value, especially when betting on the outcome as in the game of Craps. For this assignment we will simulate the rolling of a pair of dice using a pseudorandom number generator. It is called "pseudorandom" because it is the result of an algorithm, but the sequence of numbers that it generates has no discernible pattern.
In C#, the pseudorandom number generator is accessed using the following code:
// Declare and create a random number object // Do this once at the beginning of the program Random r = new Random(); // Get a random integer in range [1, 7). Note that the range // is exclusive at the maximum end, so the possible values are // 1 through 6 int number number = r.Next(1, 7);
Assignment
Write a C# Windows Application program to simulate throwing a pair of dice a user input number of times. After completing the throws, the program should display the number of throws for each roll value and the average roll value.
The application interface should consist of a textbox to allow the user to input the number of throws in the simulation, a button to run the simulation, and a listbox to display the results. An example of this interface is shown below. Note that since we are using the pseudorandom number generator, each time we run the application the reported numbers will be different. However, they should be in the same range as the example, and the average should be around 7. All of the code should go into the handler for the button.
The application must use an array of integers indexed by the roll value (2 through 12) to keep track of the number of times that roll value has been thrown. Here is some pseudocode to get you started:
Declare and create an array of integers of sufficient size
Declare and create a Random object as shown above
Use a loop to initialize the array elements to 0
Initialize a total to 0
Get the user input from the textbox (number of throws)
Use a loop to simulate throwing a pair of dice for the number of throws input
Use the Random object to get two random integers between 1 and 6
Sum the two numbers to get the rollValue
Use the rollValue to increment the appropriate array element
Add the rollValue to the total
Use a loop to display the number of times each roll value was thrown from the array
Compute and display the average rollValue

Programming notes:
The input textbox, the button, and the results listbox must be renamed to descriptive names
There must be enough labeling for a user to understand what she is expected to input.
The form must have an appropriate titlebar.
What to submit
For full credit, a compressed (zipped) solution folder containing a C# project must be submitted as an attachment to an email to Dr. Hwang (hwang@evansville.edu) no later than 4:30pm on the due date.
To create a compressed solution folder find the solution folder via Windows Explorer. Right-click on the solution folder, select Send To, then select Compressed (zipped) folder. This will created a compressed folder of the same name as the solution folder with extension .zip and an icon of a folder with a zipper.
02/20/08