# Monte Carlo Method

• January 20th, 2012, 08:35 PM
audreyleone
Monte Carlo Method
Hello, I have this assignment that I don't understand at all. here are the instructions:

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.

and here is what i have so far:

Code java:

```//import the classes import java.io.IOException; import java.util.Scanner; import java.io.File; import java.util.Random; public class montecarlomethod { public static void main(String[] args) throws IOException { Scanner in = new Scanner(System.in); Random rand = new Random();   int count = 0;   System.out.print("Number of Trials: "); int numTrials = in.nextInt();   //randomly generate the value of the "guessing" cap int randBottleCap = rand.nextInt(5)+1;   //Loop to increment through the number of trials for(int i = 1; i <= numTrials; i++) { int winningCap = rand.nextInt(5)+1;   //check to see if the "guessing" cap is equal to the winning cap if( randBottleCap != winningCap) { count++; randBottleCap = rand.nextInt(5)+1; } else { System.out.println(count); }   } } }```
• January 20th, 2012, 09:00 PM
helloworld922
Re: Monte Carlo Method
What specifically don't you understand?

For a start, what is your code doing? Is there any compiler or runtime errors? If so, please post the message. What outputs are you getting, and what inputs are you using?