What's the most common digit in a random number?

It is the digit 1 (one)

Code below calculates the probability, and of course this isn't any random generator, but rather Random.Next() in C#. Number #1 wins by far (close to 80%) leaving the second place (#2, no pun intended) way behind with 64% probability. I'm sure that the mathematical explanation is trivial...

Cheers, ACC


foreach (char digit in "0123456789")
{
    Random rd = new Random();
    int count = 0;
    int M = 10000000;
    for (int i = 0; i < M; i++)
    {
        int n = rd.Next();
        string temp = n.ToString();
        if (temp.Contains(digit))
        {
            count++;
        }
    }

    Console.WriteLine("Probability to contain {0}: {1}%", digit, count * 100.0 / M);
}


Comments

Popular posts from this blog

Changing the root of a binary tree

ProjectEuler Problem 719 (some hints, but no spoilers)

The Power Sum, a recursive problem by HackerRank