dimanche 4 juin 2017

Weird standard deviation using rand()

I am trying to write a program that generates random numbers in specific range, to be used later on as input to another system. Basically to see how my program works on randomly data picked uniformly.

I've read somewhere that rand() function isn't reliable for this cause for some reason. I am using this rand() function.

This is my code: I generate 200*1000 random numbers using rand(), calculate the average, then calculate the Standard Deviation.

I am getting standard deviation = 8743441296.189... which seems very strange the least to say ? The average\expectation is 10,000 whereas I expected it to be around ~16,000. Which is already incorrect.

What went incorrect ? did I pick too little values ? is the range bad ?

I did the same in Matlab and observed an expectation of ~16,000 So the formulas are indeed correct.

What is the problem ? side effect ?

#include <stdio.h>
#include <math.h>

#define MAX 32767

int main()
{
    int arr[200][1000];
    int pickedVals[32767] = {0};
    int i = 0, j = 0;
    double average=0;
    double std=0;
    int sum;
    /*
    * picking random values
    */
    for (i=0; i<200; i++)
    {
        for (j=0; j<1000; j++)
        {
            arr[i][j] = rand();
        }
    }

    /*
    * Counting number of appearances of each instance and average
    */
    for (i=0; i<200; i++)
    {
        for (j=0; j<1000; j++)
        {
            pickedVals[arr[i][j]] = pickedVals[arr[i][j]] + 1;
            average = average + arr[i][j];
        }
    }

    /*
    * Calculating the standard deviation
    */
    for (i=0; i<MAX; i++)
    {
        std = std + pow(pickedVals[i]*(i-average), 2);
    }
    std = std/(200*1000);
    std = sqrt(std);
    average = average/MAX;
    printf("%f\n", std);
    return 0;
}




Aucun commentaire:

Enregistrer un commentaire