mercredi 10 août 2016

Accuracy of Random

I am trying to run some test of convergence of random
The average of 0, 1, 2 is 1 so the long run I would expect average minus 1 to go to zero
It is not going to zero - even with Unit16.max
I am getting like off by 300

Am I not running a big enough sample? Or is random off?
Or is something wrong with my program?

// private static System.Security.Cryptography.RNGCryptoServiceProvider rngCsp = new System.Security.Cryptography.RNGCryptoServiceProvider();
// this was not better than Random

Random ran = new Random();
public void RollEm()
{
    int sum = 0;
    int[] sums = new int[3];
    int val;
    int minVal = 0;
    int maxVal = 0;
    for (UInt32 i = 0; i < Int16.MaxValue; i++)
    {
        val = ran.Next(3);
        //val = (int)RollDice(3) - 1;
        sums[val]++;
        sum += val;
        sum--;
        if (sum < minVal)
            minVal = sum;
        if (sum > maxVal)
            maxVal = sum;

        if(i % 100 == 0)
            System.Diagnostics.Debug.WriteLine("i {0}  sum {1}  val {2}", i, sum, val);
        //System.Threading.Thread.Sleep(10);
    }
    System.Diagnostics.Debug.WriteLine("sum {0}   min {1}  max {2}", sum, minVal, maxVal);
    System.Diagnostics.Debug.WriteLine("sums {0}  {1}   {2}", sums[0], sums[1], sums[2]);
}




Aucun commentaire:

Enregistrer un commentaire