Anonymous user
Monte Carlo methods: Difference between revisions
m
→{{header|Java}}: Added clarification on the size of the shapes
(Added per request) |
m (→{{header|Java}}: Added clarification on the size of the shapes) |
||
Line 18:
int inCircle= 0;
for(int i= 0;i < numThrows;i++){
//a square with a side of length 2 centered at 0 has
//x and y range of -1 to 1
double randX= (Math.random() * 2) - 1;//range -1 to 1
double randY= (Math.random() * 2) - 1;//range -1 to 1
//distance from (0,0) = sqrt((x-0)^2+(y-0)^2)
double dist= Math.sqrt(randX * randX + randY * randY);
if(dist < 1){//circle with diameter of 2 has radius of 1
inCircle++;
}
|