Search code examples
c++randomlinear-regressionexponential-distribution

Generate random exponential value correctly c++


I want to generate random number belonging to an exponential distribution. I wrote this

    int size = atoi(argv[2]);
    double *values = (double*)malloc(sizeof(double)*size);

    double gamma = atof(argv[1]);
    if(gamma<=0.0){
        cout<<"Insert gamma"<<endl;
        return 0;
    }


    for(int i=0; i<size; i++){
        values[i]=0;

    }

    srand ( time(NULL) );
    for(int i=0; i<size; i++){
        x = ((double) rand() / (RAND_MAX));
        //cout << random <<endl;
        value=(log(1.0-x)/(-gamma));
        //count each value
        values[value]=values[value]+1.0;
    }

But they do not cover all the vector's size. More or less they cover 10% of the vector, the other fields are all 0 and due to the fact that after I need to do a linear interpolation I want to reduce those 'empty space' in order to have at least one value for each cell of the array, How can I do it? for example I have a vector of 100000 only the first 60 fields are filled with values so cells from 60 to 999999 are all 0 and when I do linear regression they impact negatively on formula.


Solution

  • Ok, I see the bug

    You'er generating size number of events. You really need more events to fill the histogram

    PS

    Probability for fill bin #n (n is in the range [0...size)) is given by expression

    prob = exp(-gamma*n) - exp(-gamma*(n+1))
    

    which for gamma equal to 0.01 and, say, n about 1000 will give you probability of about 4*10^-7. So to get even one event in this bin you'll need to sample about 2.5million times

    PPS

    and using library exponential sampling while it is good in general, won't buy you anything because as far as I know you sampling is ok