# Calculating PI in Java

Page 1 of 1

## 2 Replies - 16284 Views - Last Post: 25 September 2008 - 12:44 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=65095&amp;s=dc50ff6ea9260ab411eac96261aee8c4&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 aagrawal84

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

• Expert Schmexpert...

Reputation: 235
• 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.

### #3 Hary

• D.I.C Regular

Reputation: 44
• 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());