CSE 131 Module 2: Choice & Iteration

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


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
    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
  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:

  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.

Submitting your work (read carefully)



Last modified 12:48:21 CDT 30 August 2017
When you done with this studio, you must be cleared by the TA to receive credit.

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

You must select one of the options below
The work submitted here was performed in accordance with this course's policy on collaboration.
On your honor, you have neither given nor received any unauthorized aid on this assignment.

However, the following TAs, students, or professors were supportive in completing this assignment.
Their help was also in accordance with course policies.

Thanks to (leave blank if appropriate):

In spite of seeking help as allowable by this course's policy on collaboration, you were unable to complete this assignment. No credit will be received for this assignment.

You would like to be contacted by an instructor to facilitate staying on track in this course.

Comments about this:

You have NOT abided by this course's policy on collaboration. No credit will be received for this assignment, but by checking this box, no academic integrity violation will be filed for this assignment.

You would like to be contacted by an intructor to faciliate staying on track in this course.

Comments about this:


TA: Password: