## CSE 131 Module 2: Choice & Iteration

Important!

Before beginning any work, do a Team...Pull in your repository.

### Studio

Review studio procedures before starting.

Feel free to participate in a different group than last time. This is totally up to you, but try to find a group that makes it easy for you to participate.

Be careful how you use the web. You are required to develop solutions as a group by thinking not by finding solutions that have been thought out by others. You must be able to explain anything that you have done.

### Warmup

• First, form a group of two people, possibly three if there is an odd number in the room.
• All but one member of your group should have this web page open so you can follow along and see the instructions as you work.
• All members of the group should update their repositories:
• Open your repository in eclipse
• Right-click (control-click on a mac) on your repository name
• Drag down to Team...
• Choose Pull
• Supply your bitbucket credentials as needed
• Plan to have one computer at which your team does its work. Initially, one of you will be in charge of typing at that computer.
• Throughout the studio, you should trade who is in charge of the keyboard. Before doing so, commit your work to make sure your work is saved.

Two problems follow. First pick the one of greatest interest to your group, and try to finish that one. If you have time, then try the other one.

### Problem 1: Gambler's Ruin

#### Background

In this assignment, you will simulate the Gambler's Ruin problem. The problem is as follows: you are a gambler given some initial amount of money, and each time you gamble, you either win or lose \$1 based on your win probability. You have some goal in mind, that once reached, you stop gambling, or if you lose all of your money, you also stop gambling. What is the probability that you lose all of your money?

#### Procedure

1. With your repository open, create a new class in the studio2 package.
This may be the first new Class you have developed, but eclipse makes it easy:
• Right-click on the package name in which you want to define the new class. In this case, use studio2.
• Click New...
• Choose Class
• Pick the name Ruin for this class. Java style dictates that its classes should begin with a capital letter!
• Be sure to check the box that generates public static void main. Otherwise you will have to type that by hand.
2. Prompt the user for the following inputs:
If you don't remember how to do this, look at code from a previous studio or lab.
startAmount
winChance
The probability that you win a gamble
winAmount
If you reach this amount of money, then you won
totalPlays
The number of times you simulate the problem
3. Next, calculate the chance that you ruin using the following equations. If you are given the chance of winning, what is the chance of losing. This probability is labeled lossChance.
if (lossChance != winChance) Ruin = ((lossChance/winChance)startAmount - (lossChance/winChance)winAmount) / (1 - (lossChance/winChance)winAmount)

if (lossChance == winChance)
Ruin = 1 - startAmount / winAmount
4. Simulate the Gambler's Ruin totalPlays times. For each simulation, continue gambling until you either reach your goal or you ruin. For each simulation, print the simulation number, the number of rounds that simulation played for, and whether you won or lost.
5. Finally, print the total number of losses, simulations, your actual ruin rate, and your expected ruin rate.

Given this input:
 startAmount 12 winChance 0.25 winAmount 15 totalPlays 31
Your final output should look similar to this:
```Simulation 1: 22 rounds  	LOSE
Simulation 2: 18 rounds  	LOSE
Simulation 3: 20 rounds  	LOSE
Simulation 4: 42 rounds  	LOSE
Simulation 5: 24 rounds  	LOSE
Simulation 6: 22 rounds  	LOSE
Simulation 7: 24 rounds  	LOSE
Simulation 8: 48 rounds  	LOSE
Simulation 9: 16 rounds  	LOSE
Simulation 10: 26 rounds  	LOSE
Simulation 11: 32 rounds  	LOSE
Simulation 12: 18 rounds  	LOSE
Simulation 13: 22 rounds  	LOSE
Simulation 14: 24 rounds  	LOSE
Simulation 15: 18 rounds  	LOSE
Simulation 16: 18 rounds  	LOSE
Simulation 17: 26 rounds  	LOSE
Simulation 18: 18 rounds  	LOSE
Simulation 19: 34 rounds  	LOSE
Simulation 20: 22 rounds  	LOSE
Simulation 21: 48 rounds  	LOSE
Simulation 22: 26 rounds  	LOSE
Simulation 23: 40 rounds  	LOSE
Simulation 24: 18 rounds  	LOSE
Simulation 25: 20 rounds  	LOSE
Simulation 26: 20 rounds  	LOSE
Simulation 27: 34 rounds  	LOSE
Simulation 28: 22 rounds  	LOSE
Simulation 29: 22 rounds  	LOSE
Simulation 30: 3 rounds  	WIN
Simulation 31: 24 rounds  	LOSE

Losses: 30  Simulations: 31
Actual Ruin Rate: 0.967741935483871  Expected Ruin Rate: 0.9629630300735122
```

### Problem 2: Computing Pi by throwing darts

Computer scientists often use simulation as a means of modeling, understanding, and predicting real-world phenomena.

Your group is auditioning for Survivor by proving your group's ability to compute Pi using only the materials at hand, as follows:

• A unit-square dart board (say, 1 meter by 1 meter). Unit-square dart boards are astoundingly resilient in plane crashes, and yours is nicely intact.
• Some darts, suitable for throwing at the dart board.
• A string and a stylus, standard safety-kit issue, suitable for inscribing a unit circle in your unit-square dartboard.
• A dart-throwing expert. However, since the plane crash, the expert is left with the (uncanny) ability to throw darts that always land somewhere, uniformly and randomly, within the unit-square dart board.
While the thrower never misses the unit square, the darts land sometimes within the inscribed circle, sometimes not.
1. As a group, develop an approach for computing Pi based on the above materials.
2. Implement your approach using iteration. You can start with the following Pi.java file that you can paste into a new Java class in one of your lab projects.
This may be the first new Class you have developed, but eclipse makes it easy:
• Right-click on the package name in which you want to define the new class. In this case, use studio2.
• Click New...
• Choose Class
• Pick the name Pi for this class, since the code you will paste is for class Pi. Java style dictates that its classes should begin with a capital letter!
• When the editor opens for your new class, copy and paste the code from Pi.java into the class.

You will need to simulate a random dart thrower. The function Math.random() will help, as it returns a nonnegative double less than 1.0. You may also find the Math.sqrt() function to be helpful.

3. Investigate and discuss how well your technique computes Pi.

### Further investigations

If you have time, pick one or both of the following:

1. Investigate the fairness of the Math.random() method.
1. What normative criteria should a random number possess?
2. How can you measure the fairness of a random number generator?
3. Implement some tests and discuss your results amongst yourselves and other groups.
2. There are other ways of computing Pi. Try some of these and study their effectiveness in terms of the number of iterations you use.

• If your studio contains a feedback.txt file, respond to the questions and supply any requested information.
• You must commit all of your work to your repository. It's best to do this from the top-most level of your repository, which bears your name and student ID.

When you are done with this studio, you must be cleared by the TA to receive credit.
• Do a Team…Pull to update your repository. You must do this or the commit/push below may fail.
Make certain this has worked by logging into bitbucket. There you will see the commit(s) in your news feed if it was successful. You can also check the Source page to locate and ensure your code was received.

It is your responsibility to make certain the code has been pushed. Some of your work receives credit through testing of your pushed code. You will receive no credit for such work if you failed to push. We generally reserve the right to revoke credit for any of your work that has not been pushed on-time.

• Fill in the form below with the relevant information
• Have a TA check your work
• The TA should check your work and then fill in the TA's name
• Click OK while the TA watches
• If you request propagation, it does not happen immediately, but should be posted in the next day or so

This demo box is for studio 2
 Last name WUSTL Key Propagate? (NOT your numeric ID) Do not propagate lower case only e.g. Smith j.smith 1 Copy from 1 to all others 2 Copy from 2 to all others 3 Copy from 3 to all others 4 Copy from 4 to all others

Acknowledgements and assertion of integrity