14 Replies - 1002 Views - Last Post: 02 October 2011 - 06:33 PM Rate Topic: -----

#1 milorambaldi  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1820
  • View blog
  • 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?
Was This Post Helpful? 0
  • +
  • -

#3 milorambaldi  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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

Was This Post Helpful? 0
  • +
  • -

#4 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1820
  • View blog
  • 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?
Was This Post Helpful? 0
  • +
  • -

#5 milorambaldi  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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
Was This Post Helpful? 0
  • +
  • -

#6 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1820
  • View blog
  • 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
Was This Post Helpful? 0
  • +
  • -

#7 milorambaldi  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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 π.
Was This Post Helpful? 0
  • +
  • -

#8 milorambaldi  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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
Was This Post Helpful? 0
  • +
  • -

#9 pbl  Icon User is offline

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

Reputation: 8347
  • View blog
  • Posts: 31,910
  • 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 :)
Was This Post Helpful? 0
  • +
  • -

#10 milorambaldi  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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)
Was This Post Helpful? 0
  • +
  • -

#11 pbl  Icon User is offline

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

Reputation: 8347
  • View blog
  • Posts: 31,910
  • 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;
Was This Post Helpful? 0
  • +
  • -

#12 milorambaldi  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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
Was This Post Helpful? 0
  • +
  • -

#13 pbl  Icon User is offline

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

Reputation: 8347
  • View blog
  • Posts: 31,910
  • 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);
	}
}


Was This Post Helpful? 1
  • +
  • -

#14 milorambaldi  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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
Was This Post Helpful? 0
  • +
  • -

#15 burakaltr  Icon User is offline

  • D.I.C Regular

Reputation: 92
  • View blog
  • Posts: 276
  • 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

View Postmilorambaldi, 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.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1