Ejemplo n.º 1
0
        public void EstimatePI(int dartCount, int fileCount)
        {
            DateTime StartTime = DateTime.Now;

            DartCount      = dartCount;
            ResultFileName = $"EstimatePIResult_{fileCount}.csv";

            // will stop if error during setup
            if (!string.IsNullOrWhiteSpace(Setup()))
            {
                return;
            }

            // everything ok in the setup, continue
            EstimatedPIResult estimatedPIResult = DoEstimate();

            DateTime EndTime = DateTime.Now;

            FileInfo fi = new FileInfo($"{TempDirPath}{ResultFileName}");

            TimeSpan timeSpan = new TimeSpan(EndTime.Ticks - StartTime.Ticks);

            StreamWriter sw = fi.CreateText();

            sw.WriteLine($"Count,InCircle,PI,Seconds");
            sw.WriteLine($"{estimatedPIResult.Count},{estimatedPIResult.InCircle},{estimatedPIResult.EstimatedPI},{timeSpan.Seconds}");
            sw.Close();
        }
Ejemplo n.º 2
0
        private EstimatedPIResult DoEstimate()
        {
            EstimatedPIResult estimatedPIResult = new EstimatedPIResult();

            Random r = new Random();

            int Count    = 0;
            int InCircle = 0;

            for (int i = 0; i < DartCount; i++)
            {
                double x = r.NextDouble();
                double y = r.NextDouble();

                if (IsInCircle(x, y))
                {
                    InCircle++;
                }

                Count++;
            }

            estimatedPIResult.Count       = Count;
            estimatedPIResult.InCircle    = InCircle;
            estimatedPIResult.EstimatedPI = 4.0D * (double)InCircle / (double)Count;

            return(estimatedPIResult);
        }