Studio 2: Gambler's Ruin and Pi
- Warmup
- Problem 1: Gambler's Ruin
- Problem 2: Computing Pi by throwing darts
- Further investigations
- Submitting your work:
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:
- 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.
- 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.
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
- With your repository open, create a new class in the studio2 package.
- Right click 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
Ruinfor 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.
- Right click Right-click on the package name in which you want to define the new class. In this case, use
-
Have your program accept the following inputs:
If you don't remember how to do this, look at code from a previous studio or lab.
startAmount- The amount of money that you start with
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
-
Next, calculate the chance that you ruin.
if (lossChance != winChance)
Ruin = ((lossChance/winChance)startAmount - (lossChance/winChance)winAmount) / (1 - (lossChance/winChance)winAmount)
if (lossChance == winChance)
Ruin = 1 - startAmount / winAmount - 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.
- 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.
- As a group, develop an approach for computing Pi based on the above materials.
- 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.
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 theMath.sqrt()function to be helpful. 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
Pifor this class, since the code you will paste is for classPi. 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.
- Right-click on the package name in which you want to define the new class. In this case, use
- Investigate and discuss how well your technique computes Pi.
Further investigations
If you have time, pick one or both of the following:
- Investigate the fairness of the
math.random()method.- What normative criteria should a random number possess?
- How can you measure the fairness of a random number generator?
- Implement some tests and discuss your results amongst yourselves and other groups.
- There are other ways of computing Pi. Try some of these and study their effectiveness in terms of the number of iterations you use.
Submitting your work:
To submit your work, contact the instructor or a TA. They will walk you through the submission process. Be prepared to show them the work that you have done and answer their questions about it!