- #1

Indigo_Blue

- 5

- 0

## Homework Statement

There is a square and a circle is inscribed inside. Simulate an experiment where raindrops drop on this surface. Some raindrops can drop in or on the border of the circle. Some can drop outside the circle. The area of the square divided by the area of the circle gives you pi. If we manipulate this equation using the amount of times a raindrop drops into or outside the circle, we can approximate pi. The calculated value of pi is supposed to become more accurate as we run the simulation more. However, I am always getting these values:

4.695652173913044

4.333333333333333

4.321198252548367

4.378718056137411

4.337363058811759

The number does not seem to get closer to pi even if I increase the amount of times the loop iterates. I cannot find the error that would make this number off from pi.

## Homework Equations

(x – h)^2 + (y – k)^2 = r^2 is the equation of a circle.

## The Attempt at a Solution

There are two classes in my code.

**Mentor note**: I added code tags to make your code more readable. In future posts involving Java code, you should add code tags, like so:

[code=java]<Your java code

.

.

.

[/code]

Main class

Java:

```
public class Main {
public static void main(String[] args) {
MonteCarlo circle=new MonteCarlo(5,3,2);
int cirCount=0; int sqrCount=0; double area=(Math.pow(2*circle.r,2));
for(int x=0;x<100;x++)
{
if(circle.insideCircle(circle.nextRainDrop_x(),circle.nextRainDrop_y())==true)
cirCount++;
else
sqrCount++;
}
System.out.println((cirCount*(area))/(sqrCount*circle.r*circle.r));
cirCount=0; sqrCount=0;
for(int x=0;x<1000;x++)
{
if(circle.insideCircle(circle.nextRainDrop_x(),circle.nextRainDrop_y())==true)
cirCount++;
else
sqrCount++;
}
System.out.println(cirCount*(area)/(sqrCount*(circle.r*circle.r)));
cirCount=0; sqrCount=0;
for(int x=0;x<10000;x++)
{
if(circle.insideCircle(circle.nextRainDrop_x(),circle.nextRainDrop_y())==true)
cirCount++;
else
sqrCount++;
}
System.out.println(cirCount*(area)/(sqrCount*circle.r*circle.r));
cirCount=0; sqrCount=0;
for(int x=0;x<100000;x++)
{
if(circle.insideCircle(circle.nextRainDrop_x(),circle.nextRainDrop_y())==true)
cirCount++;
else
sqrCount++;
}
System.out.println(cirCount*(area)/(sqrCount*circle.r*circle.r));
cirCount=0; sqrCount=0;
for(int x=0;x<1000000;x++)
{
if(circle.insideCircle(circle.nextRainDrop_x(),circle.nextRainDrop_y())==true)
cirCount++;
else
sqrCount++;
}
System.out.println((cirCount*(area))/(sqrCount*circle.r*circle.r));
}
}
MonteCarlo class
import java.util.*;
public class MonteCarlo{
public double h;
public double k;
public double r;
private Random rndm= new Random();
public MonteCarlo(double x,double y, double z)
{
h=x; k=y; r=z;
}
public double nextRainDrop_x()
{
int lowestXvalue;
int highestXvalue;
if(h != (int)h){
double amountOfDecimalPlaces=Math.pow(10,String.valueOf(h).split("\\.")[1].length());
int newh=(int)(h*amountOfDecimalPlaces);
int newr=(int)(r*amountOfDecimalPlaces);
lowestXvalue=newh-newr;
highestXvalue=newh+newr;
int PossibleXvalue=lowestXvalue+rndm.nextInt(highestXvalue+1-lowestXvalue);
return PossibleXvalue/amountOfDecimalPlaces;
}
else
lowestXvalue=(int)(h-r);
highestXvalue=(int)(h+r); // --> Possible error here
return lowestXvalue+rndm.nextInt(highestXvalue+1-lowestXvalue);
}
public double nextRainDrop_y()
{
int lowestXvalue;
int highestXvalue;
if(k != (int)k)
{
double amountOfDecimalPlaces=Math.pow(10,String.valueOf(k).split("\\.")[1].length());
int newh=(int)(k*amountOfDecimalPlaces);
int newr=(int)(r*amountOfDecimalPlaces);
lowestXvalue=newh-newr;
highestXvalue=newh+newr;
int PossibleXvalue=lowestXvalue+rndm.nextInt(highestXvalue+1-lowestXvalue);
return PossibleXvalue/amountOfDecimalPlaces;
}
else
lowestXvalue=(int)(k-r);
highestXvalue=(int)(k+r); // --> Possible error here, too
return lowestXvalue+rndm.nextInt(highestXvalue+1-lowestXvalue);
}
public boolean insideCircle(double x,double y){
if((x-h)*(x-h)+(y-k)*(y-k)<=r*r)
return true;
return false;
}
}
```

*Please run my code and figure out where the error. I don't understand what I'm doing wrong.*

Last edited by a moderator: