Class ComputePi
- java.lang.Object
-
- org.apache.commons.rng.examples.quadrature.MonteCarloIntegration
-
- org.apache.commons.rng.examples.quadrature.ComputePi
-
public class ComputePi extends MonteCarloIntegration
Computation of \( \pi \) using Monte-Carlo integration. The computation estimates the value by computing the probability that a point \( p = (x, y) \) will lie in the circle of radius \( r = 1 \) inscribed in the square of side \( r = 1 \). The probability could be computed by \[ area_{circle} / area_{square} \], where \( area_{circle} = \pi * r^2 \) and \( area_{square} = 4 r^2 \). Hence, the probability is \( \frac{\pi}{4} \). The Monte Carlo simulation will produce \( N \) points. Defining \( N_c \) as the number of point that satisfy \( x^2 + y^2 \le 1 \), we will have \( \frac{N_c}{N} \approx \frac{\pi}{4} \).
-
-
Constructor Summary
Constructors Constructor Description ComputePi(org.apache.commons.rng.UniformRandomProvider rng)
Create an instance.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description double
compute(long numPoints)
Compute the value of pi.protected boolean
isInside(double... rand)
Indicates whether the given points is inside the region whose integral is computed.static void
main(String[] args)
Program entry point.-
Methods inherited from class org.apache.commons.rng.examples.quadrature.MonteCarloIntegration
integrate
-
-
-
-
Constructor Detail
-
ComputePi
public ComputePi(org.apache.commons.rng.UniformRandomProvider rng)
Create an instance.- Parameters:
rng
- RNG.
-
-
Method Detail
-
main
public static void main(String[] args)
Program entry point.- Parameters:
args
- Arguments. The order is as follows:- Number of random 2-dimensional points to generate.
-
Random source identifier
.
-
compute
public double compute(long numPoints)
Compute the value of pi.- Parameters:
numPoints
- Number of random points to generate.- Returns:
- the approximate value of pi.
-
isInside
protected boolean isInside(double... rand)
Indicates whether the given points is inside the region whose integral is computed.- Specified by:
isInside
in classMonteCarloIntegration
- Parameters:
rand
- Point whose coordinates are random numbers uniformly distributed in the unit interval.- Returns:
true
if thepoint
is inside.
-
-