コード例 #1
0
        private static void LowDiscrepancySequenceApproach()
        {
            do
            {
                int length = UI.AcceptValidInt("Enter how many points to generate: (0 to quit, max 2147483637) \n>", 0, int.MaxValue - 10);
                if (length == 0)
                {
                    break;
                }
                int eachstep = UI.AcceptValidInt("Enter how many points per step to print (Put bigger steps >10000000 to avoid screen flash): (0 to quit) \n>", 0, int.MaxValue - 50);
                if (eachstep == 0)
                {
                    break;
                }

                int       inside    = 0;
                int       total     = 0;
                Stopwatch stopwatch = new Stopwatch();
                stopwatch.Start();
                foreach (var v in QuasiRandom.SobolSequence(2, length))
                {
                    if (hypotenuseSquare(new XYCoord(v[0], v[1])) < 1)
                    {
                        inside++;
                    }
                    total++;
                    if (total % eachstep == 0)
                    {
                        Console.WriteLine($"{inside}, {total}, {(double)inside / total * 4}             Elasped time:{stopwatch.Elapsed.TotalSeconds} Seconds");
                    }
                }
                decimal estimatePi = (decimal)inside / total * 4;
                Console.WriteLine($"================================================================\n" +
                                  $"Estimated Pi Value:  {estimatePi:f14}\n" +
                                  $"Actual Pi Value:     {Math.PI}\n" +
                                  $"Difference:          {Math.Abs(estimatePi - (decimal)Math.PI):f14}\n" +
                                  $"Elasped time:        {stopwatch.Elapsed.TotalSeconds} Seconds\n" +
                                  $"Inside Pts Generated:{inside}\n" +
                                  $"Total  Pts Generated:{total}\n" +
                                  $"================================================================\n");
            } while (true);
        }
コード例 #2
0
ファイル: RandomGenerator.cs プロジェクト: zedr0n/ZES.BDO
        public static void Initialise(int numberOfPaths)
        {
            _nPaths = numberOfPaths;
#if USE_THIRD_PARTY_RANDOM_GENERATORS
            if (UseQuasiRandom)
            {
                if (UseExtreme)
                {
                    _enumerator = QuasiRandom.HaltonSequence(Dimension, numberOfPaths).GetEnumerator();
                }
                else
                {
                    var generator = new SobolQuasiRandomGenerator(Dimension);
                    _generatedRandoms = generator.Next(new DoubleRandomUniformDistribution(), numberOfPaths);
                }

                return;
            }
#endif
            var random         = new Random();
            var useAntithetic  = UseAntithetic && _nPaths % 2 == 0;
            var nPaths         = useAntithetic ? _nPaths / 2 : _nPaths;
            var defaultRandoms = new List <List <double> >();
            for (var i = 0; i < nPaths; ++i)
            {
                var list = new List <double>();
                for (var j = 0; j < Dimension; ++j)
                {
                    list.Add(random.NextDouble());
                }

                defaultRandoms.Add(list);
                if (useAntithetic)
                {
                    defaultRandoms.Add(list.Select(x => 1.0 - x).ToList());
                }
            }

            _defaultEnumerator = defaultRandoms.GetEnumerator();
        }