Welcome to Dream.In.Code
Become a Java Expert!

Join 149,581 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,764 people online right now. Registration is fast and FREE... Join Now!




Confused with calculating PI using dart! HELP

 
Reply to this topicStart new topic

Confused with calculating PI using dart! HELP

utastn2009
11 Oct, 2007 - 08:12 PM
Post #1

New D.I.C Head
*

Joined: 4 Oct, 2007
Posts: 2


My Contributions
Okay I got my last assignment about calculating the square root, but this assignment just has me really confused. I'm not sure how I'm suppose to use Monte Carlo's method. Here's what I have to do.

Imagine that the square dart board has a coordinate system attached to it. The upper right corner has coordinates ( 1.0, 1.0) and the lower left corner has coordinates ( -1.0, -1.0 ). It has sides that are 2 units long and its center (as well as the center of the inscribed circle) is at the origin.

A random point inside the dart board can be specified by its x and y coordinates. These values are generated using the random number generator. There is a method nextDouble() that will return a double between 0.0 (inclusive) and 1.0 (exclusive). But we need random numbers between -1.0 and +1.0. The way we achieve that is:

double xPos = (randomGen.nextDouble()) * 2 - 1.0;
double yPos = (randomGen.nextDouble()) * 2 - 1.0;


To determine if a point is inside the circle its distance from the center of the circle must be less than the radius of the circle. The distance of a point with coordinates ( xPos, yPos ) from the center is Math.sqrt ( xPos * xPos + yPos * yPos ). The radius of the circle is 1 unit.

In your method main() you want to experiment and see if the accuracy of PI increases with the number of throws on the dartboard. You will compare your result with the value given by Math.PI. The quantity Difference in the output is your calculated value of PI minus Math.PI. Use the following number of throws to run your experiment - 100, 1000, 10,000, and 100,000. You will call the method computePI() with these numbers as input parameters. Your output will be of the following form:

Computation of PI using Random Numbers

Number of throws = 100, Computed PI = ..., Difference = ...
Number of throws = 1000, Computed PI = ..., Difference = ...
Number of throws = 10000, Computed PI = ..., Difference = ...
Number of throws = 100000, Computed PI = ..., Difference = ...

* Difference = Computed PI - Math.PI


In the method computePI() you will simulate the throw of a dart by generating random numbers for the x and y coordinates. You will call the method isInside() to determine if the point is inside the circle or not. This you will do as many times as specified by the number of throws. You will keep a count of the number of times a dart landed inside the circle. That figure divided by the total number of throws is the ratio π / 4. The method computePI() will return the computed value of PI.
So this is what I have so far..I know the structure of the program...I just don't know how to actually make it.
CODE

import java.util.Random;

public class CalculatePI
{
  public static boolean isInside ( double xPos, double yPos )
  {
    double xPos = (randomGen.nextDouble()) * 2 - 1.0;
    double yPos = (randomGen.nextDouble()) * 2 - 1.0;

  }

  public static double computePI ( int numThrows )
  {
  
  }

  public static void main ( String[] args )
  {
    Random randomGen = new Random ( System.currentTimeMillis() );

  }
}


Any suggestions on what I should do next? or add... or take out?
User is offlineProfile CardPM
+Quote Post

Martyr2
RE: Confused With Calculating PI Using Dart! HELP
11 Oct, 2007 - 09:41 PM
Post #2

Programming Theoretician
Group Icon

Joined: 18 Apr, 2007
Posts: 5,655



Thanked: 313 times
Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions
You are in luck, I have already posted a full solution to this very problem. Someone else was working on the assignment and the code I provided showed him how to do it and I am sure it will help you too.

The post is located at the following URL:

How to calculate PI using the Monte Carlo method

Enjoy! smile.gif
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/7/09 10:44PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month