Esempio n. 1
0
        static void Main(string[] args)
        {
            int    counter;
            long   numOps;
            int    inRadius;
            double ratioInOut;
            double distance;

            double[,] coordinates;
            XYCoords xySet;
            XYCoords xyStore;
            Random   randomGen = new Random();

            do
            {
                counter  = 0;
                inRadius = 0;
                Console.WriteLine("Enter number of Iterations:");
                if (long.TryParse(Console.ReadLine(), out numOps))
                {
                    coordinates = new double[numOps, 2];
                    do
                    {
                        xySet = new XYCoords(randomGen);
                        coordinates[counter, 0] = xySet.x;
                        coordinates[counter, 1] = xySet.y;
                        counter++;
                    }while (counter < numOps);
                    counter = 0;
                    do
                    {
                        xyStore  = new XYCoords(coordinates[counter, 0], coordinates[counter, 1]);
                        distance = Distance(xyStore);
                        if (distance <= 1.0)
                        {
                            ++inRadius;
                        }
                        counter++;
                    }while (counter < numOps);
                    ratioInOut = ((inRadius / 1.0) / (numOps / 1.0)) * 4.0;
                    Console.WriteLine($"Number of points inside radius: {inRadius}");
                    Console.WriteLine($"Absolute difference between estimate of pi and math.pi: {Math.Abs(ratioInOut - Math.PI)}");
                }
            }while (true);
        }
Esempio n. 2
0
 static double Distance(XYCoords xAndY) => Math.Sqrt((xAndY.x * xAndY.x) + (xAndY.y * xAndY.y));