Hello!(adsbygoogle = window.adsbygoogle || []).push({});

I am taking a computational physics class this semester and just got out of a test. One of the questions provided us with 10 random numbers and asked us to approximate pi. The method I came up with was realizing that

[tex] \int_0^1 \sqrt{1-x^2} dx = \frac{\pi}{4},[/tex]

and then performing this integral numerically. To perform the integral numerically, I plugged in the random values in place of x and evaluated the integrand. I then summed up the integrand values for each of the 10 random numbers and divided this value by 10 to give me pi/4. This method game me a value of pi of around 3.49. I figured this was good enough since I only had 10 values. However, I decided to come back and actually code this into the computer. Using java, I wrote the following code:

However, even with a 100000 random numbers I'm still only getting values of around 3.415. There seems to be some kind of systematic error in my logic, however I can't pinpoint what it is. Any help clearing up this matter would be appreciated.Code (Text):

import java.io.*;

import java.util.*;

import java.math.*;

public class Test

{

public static void main(String [] args)

{

double sum = 0;

for(int i = 0; i< 100000; i++)

{

sum+=Math.sqrt(1-(Math.random() * Math.random()));

}

sum = (sum / 100000) * 4;

System.out.println(sum);

}

}

Thanks.

**Physics Forums | Science Articles, Homework Help, Discussion**

Dismiss Notice

Join Physics Forums Today!

The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

# Calculating pi with Monte Carlo integration

**Physics Forums | Science Articles, Homework Help, Discussion**