Hi, I'm working on this project right now which probably shouldn't be that difficult but I'm really confused and lost. The assignment's instructions are: "Write a program that uses the Monte Carlo sampling method to estimate the average number of bottles of e-Boost someone would have to drink to win a prize. There is a 1 in 5 chance that a bottle cap will have a prize. When your program runs your output should simply print a message indicating the average number of bottles of e-Boost you would need to drink to win a prize.
The steps are:
1. Determine how many bottle caps each person has to open in
order to find a winning cap. (This represents one trial.)
Print this value to a text file.
2. Prompt the user for the number of trials. Conduct at least 1000 trials.
3. Read back the data for all of the trials from the output file.
4. Calculate the average number of caps opened in order to win a prize.
5. Print the result to the screen."

Here's my code so far:

import java.io.IOException;
import java.io.PrintWriter;
import java.io.File;
import java.util.Scanner;
class BottleCapPrize
{
   public static void main (String [ ] args) //throws IOException
   {
    int win = 0;
    double randNum = 0.0;
    int counter1 = 0;
    int counter2 = 0;
    int trials = 0;

    Scanner in = new Scanner(System.in);

    System.out.print("How many trials?: ");
    trials = in.nextInt();

    //PrintWriter outFile = new PrintWriter (new File("BottleCap.txt"));
    randNum = Math.random(); 
    randNum = randNum * 6;
    while(counter1 <= trials)
    {
       while(randNum != 5)
       {
           counter2++;
           System.out.println("Total bottles needed to find a winner:" + counter2);
        }
        counter1++;
    }
    System.out.println("Total bottle caps: " + counter2);
    System.out.println("Total trials conducted: " + counter1);
    //outFile.close();
   }
}

I guess I don't have a specific question, but can someone please help me get some direction on this? I don't know how to continue in this assignment, and obviously something is wrong because it just prints infinite results.

This is also supposed to print into a text file, but I figured it would be easier to print the results onto the screen until I get a finished product.

Thank you!

the infinite results is because of this:

while(randNum != 5)
{
counter2++;
System.out.println("Total bottles needed to find a winner:" + counter2);
}

you tell the application: as long as radNum is not 5, add 1 to counter2, and print this line.

that is when it goes back to the check of the condition: is radNum equal to 5?
since you didn't change the value of radNum, if the expression returned true the first time, it'll do so until your system runs out of resources and the application crashes.

while(randNum != 5)
{
counter2++;
System.out.println("Total bottles needed to find a winner:" + counter2);
 randNum = Math.random();
randNum = randNum * 6;
}

if you add this lines in the while block as well, the value of radNum will be changed and might become 5

This is also not a Monte Carlo sampling routine. You need to have a set of bottles, and then pick one at random until you find the winner. You will also need to make multiple runs and average the number of samples you had to try before finding a "winner". The roulette wheel is a great example of a true random number generator... :-)

Here is a great tutorial about the subject: http://www.phy.ornl.gov/csep/mc/mc.html

You need to have a set of bottles, and then pick one at random until you find the winner. You will also need to make multiple runs and average the number of samples you had to try before finding a "winner".

Okay, so I think I understand the basic concept so thank you for steering me in the right direction but how do I put that into practice? Sorry for these stupid questions :(

This article has been dead for over six months. Start a new discussion instead.