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

Join 150,196 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 2,035 people online right now. Registration is fast and FREE... Join Now!




Calculating PI in Java

 
Reply to this topicStart new topic

Calculating PI in Java

aagrawal84
25 Sep, 2008 - 10:21 AM
Post #1

New D.I.C Head
*

Joined: 25 Sep, 2008
Posts: 10

Hi everyone,

I'm very new to Java and we have a project to calculate PI. Here is my prompt:

Determining π Experimentally:

Recall that π is the ratio of a circle's circumference to its
diameter and that we can calculate the area of a circle with the
formula A=πr2. Below is a circle enscribed within the unit
square.
What is the ratio of the areas of the enscribed circle to that of
the unit square?

If we pick a random point within the unit square what is the
probability that the point will also lie within the circle?

If we repeat this experiment an arbitrarily large number of times 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.

Using the language structures we have discussed write a program that
will do the above experiment an arbitary (determined at run-time)
number of times and report back the approximate value of π.
---------------------------------------------------------------------------------
I did the program but for some reason, everytime I run it, I get a different value even if I type in the same number of repeats. Here is my code:

CODE

import java.util.*;

public class PI
{  
    public static boolean isInside (double x1, double y1)    
    {    
        double distance = Math.sqrt((x1 * x1) + (y1 * y1));

        return (distance < 1.0);
    }
    
    public static double computePI (int numRepeats)
    {  
        Random randomGen = new Random (System.currentTimeMillis());        
        int repeats = 0;
        double PI = 0;        
        
        for (int i = 1; i <= numRepeats; i++)
        {  
            double x1 = (randomGen.nextDouble()) * 2 - 1.0;
            double y1 = (randomGen.nextDouble()) * 2 - 1.0;
            if (isInside(x1, y1))
            {
                repeats++;
            }
        }

    double dRepeats = numRepeats;

    PI = (4.0 * (repeats/dRepeats));

        return PI;
    }

    public static void main (String[] args)
    {
        Scanner scan = new Scanner (System.in);
        System.out.print("Please enter number of times you would like to run this program: ");
        int numRepeats = scan.nextInt();
        double PI = computePI(numRepeats);
        System.out.println ("Computed PI = " + PI);        
    }
}



I'm really confused because I don't understand what I'm doing wrong. If someone could help me, I'd really appreciate it!
User is offlineProfile CardPM
+Quote Post

Gloin
RE: Calculating PI In Java
25 Sep, 2008 - 10:25 AM
Post #2

Expert Schmexpert...
Group Icon

Joined: 4 Aug, 2008
Posts: 934



Thanked: 54 times
My Contributions
You're supposed to get different results each time. It's because it's an approximation based on randomness.
User is online!Profile CardPM
+Quote Post

Hary
RE: Calculating PI In Java
25 Sep, 2008 - 11:44 AM
Post #3

D.I.C Regular
***

Joined: 23 Sep, 2008
Posts: 250



Thanked: 18 times
My Contributions
And you do really select a different random feed every run, based on the current system time.

> Random randomGen = new Random (System.currentTimeMillis());

User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/9/09 04:44AM

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