online advertising

Friday, February 12, 2016

Duck Pond Chance (2)

Continued with the last post. Here is an implementation of a Monte Carlos simulation.

            Random random = new Random(0);
            const int n = 4;
            int hitCount = 0;
            int labCount = 0;
            for (int i = 0; i < 100000; i++)
            {
                // Represents the angles of the ducks
                double[] d = new double[n];
                for (int j = 0; j < n; j++)
                {
                    // The multiplication by 2 pi is implicit here
                    d[j] = random.NextDouble();
                }
                Array.Sort(d);

                bool hit = false;
                for (int j = 1; j < n; j++)
                {
                    if (d[j] - d[j - 1] > 0.5) // p2 - p1 > pi
                    {
                        hit = true;
                        break;
                    }
                }
                if (!hit)
                {
                    if (d[0] + 1 - d[n - 1] > 0.5) // p1 + 2pi - p4 > pi
                    {
                        hit = true;
                    }
                }

                if (hit)
                {
                    hitCount++;
                }
                labCount++;
            }
            Console.WriteLine((hitCount + 0.0) / labCount);

The code output 0.49947, so we have a good reason to believe the answer is $ \frac{1}{2} $, and that's correct according to the website.

But it is unsettling, this seems too complex and guesswork. There should be a better argument about this.

It is also interesting to note how the probability change as $ n $ changes.

No comments:

Post a Comment