# probability of hit of throwing a dart in a circle incribed a square

Page 1 of 1

## 14 Replies - 1381 Views - Last Post: 02 October 2011 - 06:33 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=249473&amp;s=3296a3616906809dd4ea49abbfba70ab&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 milorambaldi

Reputation: 0
• Posts: 8
• Joined: 02-October 11

# probability of hit of throwing a dart in a circle incribed a square

Posted 02 October 2011 - 05:46 AM

```import java.util.*;

class MyPI {

public static void main(String[] args) {

double touch , x, y, set, exp;
double pi;

Scanner scan = new Scanner(System.in);
System.out.println("Count the number of times of touch");
exp = scan.nextDouble();
for (touch; set, x, y, set, exp, touch++) {

x = Math.random();
y = Math.random();

exp = Math.random() -1;
x = Math.random() -1;
}
if (Math.pow(x, 2) + Math.pow(y, 2) <= 1) {

System.out.println(" Scanner: " + input);
}
}

```

This post has been edited by macosxnerd101: 02 October 2011 - 07:38 PM
Reason for edit:: Please use code tags

Is This A Good Question/Topic? 0

## Replies To: probability of hit of throwing a dart in a circle incribed a square

### #2 smohd

• Critical Section

Reputation: 1822
• Posts: 4,627
• Joined: 14-March 10

## Re: probability of hit of throwing a dart in a circle incribed a square

Posted 02 October 2011 - 06:00 AM

So what is the problem with the code?
Where did you declare input?

### #3 milorambaldi

Reputation: 0
• Posts: 8
• Joined: 02-October 11

## Re: probability of hit of throwing a dart in a circle incribed a square

Posted 02 October 2011 - 06:22 AM

my input is suppose to start from '0' and to make a case where i can calculate pi.
am also having problem on how i can finish the code, i mean my last statements for the System.out.println method.

This post has been edited by macosxnerd101: 02 October 2011 - 07:38 PM
Reason for edit:: Removed quote

### #4 smohd

• Critical Section

Reputation: 1822
• Posts: 4,627
• Joined: 14-March 10

## Re: probability of hit of throwing a dart in a circle incribed a square

Posted 02 October 2011 - 06:47 AM

For random look at Math Class. That method return a number between 0 and 1 exclusive.
So again, what this loop needed to do for (touch; set, x, y, set, exp, touch++) { and what are these variables for? touch , x, y, set, exp.
Then why dont you use the value of exp entered by user?

### #5 milorambaldi

Reputation: 0
• Posts: 8
• Joined: 02-October 11

## Re: probability of hit of throwing a dart in a circle incribed a square

Posted 02 October 2011 - 06:56 AM

what would you suggest, am a newbee and beginning it all beginning to confuse me.. am learning on my own and don't really understand much

### #6 smohd

• Critical Section

Reputation: 1822
• Posts: 4,627
• Joined: 14-March 10

## Re: probability of hit of throwing a dart in a circle incribed a square

Posted 02 October 2011 - 07:03 AM

I can suggest something after I understand what you want to do. So you start by explaining the problem and we are happy to help

### #7 milorambaldi

Reputation: 0
• Posts: 8
• Joined: 02-October 11

## Re: probability of hit of throwing a dart in a circle incribed a square

Posted 02 October 2011 - 07:12 AM

i want to determine the ratio of the areas of an inscribed circle to that of
the unit square

pick a random point within the unit square and calculate the
probability that the point will also lie within the circle

then repeat this experiment for an arbitrarily large number of times and calculate the
ratio of the number of points which lie within the circle to the number
of points within the unit square (all of them) will approach π/4.

I WANT TO WRITE THIS USINT THE 'FOR' LOOP to report back the approximate value of π.

### #8 milorambaldi

Reputation: 0
• Posts: 8
• Joined: 02-October 11

## Re: probability of hit of throwing a dart in a circle incribed a square

Posted 02 October 2011 - 11:50 AM

Hello..so, what do you think am meant to do

### #9 pbl

• There is nothing you can't do with a JTable

Reputation: 8370
• Posts: 31,956
• Joined: 06-March 08

## Re: probability of hit of throwing a dart in a circle incribed a square

Posted 02 October 2011 - 02:21 PM

The Ellipse2D class implements Shape so it has all what you need
```	public static void main(String[] args) {
Ellipse2D.Double e = new Ellipse2D.Double(0.0, 0.0, 5.0, 5.0);
int in = 0, out = 0;
for(int i = 0; i < 10; ++i) {
double x = Math.random() * 5.0;
double y = Math.random() * 5.0;
if(e.contains(x, y))
++in;
else
++out;
}
System.out.println("In: " + in + " Out: " + out);
}

```

Run it 1,000,000 times and should should be able to determine the valeu of PI

### #10 milorambaldi

Reputation: 0
• Posts: 8
• Joined: 02-October 11

## Re: probability of hit of throwing a dart in a circle incribed a square

Posted 02 October 2011 - 03:18 PM

it didn't compile.. i got the following error message 'Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - package Ellipse2D does not exist
at MyPi.main(MyPi.java:3)

### #11 pbl

• There is nothing you can't do with a JTable

Reputation: 8370
• Posts: 31,956
• Joined: 06-March 08

## Re: probability of hit of throwing a dart in a circle incribed a square

Posted 02 October 2011 - 03:24 PM

You simply probably missed

import java.awt.geom.Ellipse2D;

### #12 milorambaldi

Reputation: 0
• Posts: 8
• Joined: 02-October 11

## Re: probability of hit of throwing a dart in a circle incribed a square

Posted 02 October 2011 - 03:32 PM

it didn't run

### #13 pbl

• There is nothing you can't do with a JTable

Reputation: 8370
• Posts: 31,956
• Joined: 06-March 08

## Re: probability of hit of throwing a dart in a circle incribed a square

Posted 02 October 2011 - 03:41 PM

And what is the error ?
"Mechanic my car does not start" kind of useless post

that works for me
```import java.awt.geom.Ellipse2D;

/*
* The Ellipse2D class implements Shape so it has a contains() mmethod
*/
public class Ellipse {

public static void main(String[] args) {
Ellipse2D.Double e = new Ellipse2D.Double(0.0, 0.0, 5.0, 5.0);
int in = 0, out = 0;
for(int i = 0; i < 100000; ++i) {
double x = Math.random() * 5.0;
double y = Math.random() * 5.0;
if(e.contains(x, y))
++in;
else
++out;
}
System.out.println("In: " + in + " Out: " + out);
}
}

```

### #14 milorambaldi

Reputation: 0
• Posts: 8
• Joined: 02-October 11

## Re: probability of hit of throwing a dart in a circle incribed a square

Posted 02 October 2011 - 04:50 PM

lol.. thanks

### #15 burakaltr

• D.I.C Regular

Reputation: 91
• Posts: 280
• Joined: 07-November 10

## Re: probability of hit of throwing a dart in a circle incribed a square

Posted 02 October 2011 - 06:33 PM

milorambaldi, on 02 October 2011 - 05:46 AM, said:

```import java.util.*;

class MyPI {

public static void main(String[] args) {

double touch , x, y, set, exp;
double pi;

Scanner scan = new Scanner(System.in);
System.out.println("Count the number of times of touch");
exp = scan.nextDouble();
for (touch; set, x, y, set, exp, touch++) {

x = Math.random();
y = Math.random();

exp = Math.random() -1;
x = Math.random() -1;
}
if (Math.pow(x, 2) + Math.pow(y, 2) <= 1) {

System.out.println(" Scanner: " + input);
}
}

```

The method you're describing is called the Monte Carlo Method. Assume that you are throwing darts randomly on a square where there is a circle inscribed inside. You've got to count the times the dart hits the circle per the times dart hits the square. If it is a square of 1 unit length of one side and the circle inscribed is given by the formulae

(x-0.5)**2 + (y-0.5)**2 =(0.5)**2

That is a circle of 0.5 unit radius centered at (0.5,0.5)

If you randomize the hits times enough and divide the hits in circle by hits in square, you will get the ratio of the circle area to square area which is (0.5**2)*Pi/(1*1)

So after setting up such an algorithm, multiply the result of the area ratios with 4. to get an approximation for PI.