0

The code below gives you a number close to pi when you run the program. It takes the user input for the number of darts you want thrown per trial, and uses that to come up with the number close to pi. I need to make the code run for a specific number of trials, but can't seem to make it work. How do I do this?

    System.out.print("How many darts per trial? ");
    int darts = in.nextInt();
    System.out.print("How many trials? ");
    int trials = in.nextInt();   

    //The code below this represents 1 trial
    for(int i = 0; i < darts; i++)
    {
        xInt[i] = (Math.random() * (upper - lower)) + lower;
        yInt[i] = (Math.random() * (upper - lower)) + lower;
        c2[i] = Math.pow(xInt[i], 2) + Math.pow(yInt[i], 2);
        c[i] = Math.sqrt(c2[i]);

        if(c[i] <= 1)
        {
            hits++;                 
        }                    
    }
    double answer = 4 * ((double)hits / (double)darts);            
    System.out.println(answer);

If I added the for-loop for the trials (see below), then it adds up all of the answers and prints the sum

for(int n = 0; n < trials; n++)
    {
        for(int i = 0; i < darts; i++)
        {
            xInt[i] = (Math.random() * (upper - lower)) + lower;
            yInt[i] = (Math.random() * (upper - lower)) + lower;
            c2[i] = Math.pow(xInt[i], 2) + Math.pow(yInt[i], 2);
            c[i] = Math.sqrt(c2[i]);  
            if(c[i] <= 1)
                hits++;
        }
    }

    double answer = 4 * ((double)hits / (double)darts);            
    System.out.println(answer);

It should print out something like this when you are finished.

Trial [1]: pi = 3.11341744
Trial [2]: pi = 3.04824237
Trial [3]: pi = 3.33281081
Trial [4]: pi = 3.40901039
Maya
  • 25
  • 5
  • Increase the value of the variable `darts`? – Rabbit Guy Aug 15 '16 at 20:52
  • _...inside of another for-loop, but that doesn't work"_ -- Show what you did and explain what doesn't work. Please read [ask] and visit the [help] to learn how to use this site. – Jim Garrison Aug 15 '16 at 20:53
  • To clarify - this is a Monte Carlo estimation of pi of which you have a specified, concrete number of darts, which you wish to run *N* times, or is it that you wish to add more darts? – Makoto Aug 15 '16 at 20:53
  • Okay - thanks for clarifying with your last edit - you're attempting to run it *N* times. Could you post the code you had with the extra loop? I have a *sneaking* suspicion it has something to do with your `nextInt` call but I'd like to be certain. – Makoto Aug 15 '16 at 20:56
  • What loop did you have that made use of the variable `trials`? Was it something along the lines of `for(int j = 0; j < trials; j++)` ? – Makoto Aug 15 '16 at 20:59
  • Okay. I've voted to close this as a dupe (which isn't to say your question is bad!), since the solution likely lies in that question. If that doesn't answer your question, ping me with @Makoto in this question and I'll respond. Don't forget to update your question with what *specifically* you'd have tried after reading the other question. – Makoto Aug 15 '16 at 21:02
  • Be clearer in this issue. Can you edit specifically what you expect to happen into your question? Are you trying to get individual tallies of different trials? – Makoto Aug 15 '16 at 21:09
  • I just re-edited my question and put up an example of what it should look like @Makoto – Maya Aug 15 '16 at 21:19
  • That's pretty much `answer` isn't it? I'll reopen your question but I would *strongly* encourage you to include the code that also factored in the loop to it as well. – Makoto Aug 15 '16 at 21:26

1 Answers1

1

You need to reset the number of hits for each trial by adding a hits=0; right after the trials for loop Furthermore move your calculation and print statement inside the for loop. Like this:

for(int n = 0; n < trials; n++)
{
    hits=0;
    for(int i = 0; i < darts; i++)
    {
        xInt[i] = (Math.random() * (upper - lower)) + lower;
        yInt[i] = (Math.random() * (upper - lower)) + lower;
        c2[i] = Math.pow(xInt[i], 2) + Math.pow(yInt[i], 2);
        c[i] = Math.sqrt(c2[i]);  
        if(c[i] <= 1)
            hits++;
    }
    double answer = 4 * ((double)hits / (double)darts);            
    System.out.println(answer);
}

This should fix it if i'm understanding you correctly.