2 Replies - 9310 Views - Last Post: 25 September 2008 - 12:44 PM Rate Topic: -----

#1 aagrawal84  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 25-September 08

Calculating PI in Java

Posted 25 September 2008 - 11:21 AM

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:

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!

Is This A Good Question/Topic? 0
  • +

Replies To: Calculating PI in Java

#2 Gloin  Icon User is offline

  • Expert Schmexpert...
  • member icon

Reputation: 235
  • View blog
  • Posts: 4,489
  • Joined: 04-August 08

Re: Calculating PI in Java

Posted 25 September 2008 - 11:25 AM

You're supposed to get different results each time. It's because it's an approximation based on randomness.
Was This Post Helpful? 0
  • +
  • -

#3 Hary  Icon User is offline

  • D.I.C Regular

Reputation: 44
  • View blog
  • Posts: 427
  • Joined: 23-September 08

Re: Calculating PI in Java

Posted 25 September 2008 - 12:44 PM

And you do really select a different random feed every run, based on the current system time.

> Random randomGen = new Random (System.currentTimeMillis());
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1