Tuesday, February 23, 2010

Probability to observe the same number calling Random.Rand()

It’s not an efficient query. Just a game…
for (int j = 1; j < 8; j++)
{
    int numElements = (int)Math.Pow(10, j);
    var numersList = Enumerable.Repeat(0, numElements).Select(
        i => Convert.ToDouble(random.Next()))
        .Distinct().OrderBy(i => i);
    double probabilityGetSameNumber = 1 - (double) numersList.Count()/numElements;
    Console.WriteLine(
        "Rand() was called {0} times. Same number was observed in {1} % cases",
        numElements, probabilityGetSameNumber);
}

Results:

Num of Rand() calls Same number observed in % of cases Cases count
10 0 0
100 0 0
1000 0 0
10000 0 0
100000 0.00002999999999997 3
1000000 0.00024100000000004 241
10000000 0.00234760000000001 23476

 

Technorati Теги: ,,,

No comments: