Search code examples
c#maththeory

central limit theorem


i want to observe central limit theorem and wrote this program.But i confused that,must i observe like that.Is there any wrong ?

xx
xxx
xxxx
xxxxx
xxxxxx
xxx
xxxx
xxx
x
x
namespace ConsoleApplication
{
    class Program
    {
        static void Main(string[] args)
        {
                Func();
        }
        public static void Func()
        {
            Random r = new Random();
            int[] d = new int [10];
            int sum;

            for (int k = 0; k < 5000; k++)
            {
                sum = 0;

                for (int i = 0; i < 50; i++)
                    sum += r.Next(0, 10000);
                Set(d, sum/50);

            }

            DispResult(d);
        }
        private static void DispResult(int[] d)
        {
            for (int i = 0; i < 10; i++)
            {
                for (int j = 0; j < d[i]; j += 1000)
                {
                    Console.Write("X");
                }
                Console.WriteLine();
            }
        }
        private static void Set(int[] d, int a)
        {

            if (a > 9000)
                d[9]++;
            else if (a > 8000)
                d[8]++;
            else if (a > 7000)
                d[7]++;
            else if (a > 6000)
                d[6]++;
            else if (a > 5000)
                d[5]++;
            else if (a > 4000)
                d[4]++;
            else if (a > 3000)
                d[3]++;
            else if (a > 2000)
                d[2]++;
            else if (a > 1000)
                d[1]++;
            else
                d[0]++;
        }
    }
}

Solution

  • It is very unclear what you're asking here but I'll take a stab at it.

    Your program simulates rolling a 10000-sided die fifty times and taking the average. You then do that 5000 times and show a histogram of the results.

    The Central Limit Theorem states that as the number of rolls increases, the histogram should more closely approximate a Gaussian distribution.

    If what you want to do is to observe the truth of the Central Limit Theorem, then I would modify your program as follows: I would make "Func" take an integer n, the number of rolls and then have the body of Main be:

    for(int n = 1; n < 10; ++n)
    {
       Func(n);
       Console.WriteLine("-----");
    }
    

    Then replace all the "50"s in Func with n.

    That way you are simulating rolling 1, 2, 3, 4... 10 dice and taking the average. When you plot the histograms you'll see that for 1, the histogram is rectangular and then it gets more and more bell shaped as n increases. That demonstrates the Central Limit Theorem.