Class 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 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:
        1. Number of random 2-dimensional points to generate.
        2. 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 class MonteCarloIntegration
        Parameters:
        rand - Point whose coordinates are random numbers uniformly distributed in the unit interval.
        Returns:
        true if the point is inside.