1

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.

Domenico
  • 19
  • 1
  • 5
  • For your example with 60 filled, what kind of `gamma` did you use? – Severin Pappadeux Mar 29 '15 at 22:39
  • 4
    [Do you actually need to write it yourself?](http://en.cppreference.com/w/cpp/numeric/random/exponential_distribution) – gha.st Mar 29 '15 at 22:44
  • Say, for `gamma=1` quick on the back of the envelope calc shows you'll have less than about 700 bins filled with double representation (smallest double is about 10^-308) – Severin Pappadeux Mar 29 '15 at 22:44
  • @SeverinPappadeux using gamma=0.01 and a vector size of 100000 now I have more or less 600 values and if I use gamma=0.3 I have 49 values – Domenico Mar 29 '15 at 22:44
  • @gha.st there are other ways? – Domenico Mar 29 '15 at 22:47
  • @Domenico The C++ standard library contains a substantial random number generation part. [As you can see here, this includes exponential distributions](http://en.cppreference.com/w/cpp/numeric/random/exponential_distribution), which are applicable to [various (P)RNGs, e.g. MT19937](http://en.cppreference.com/w/cpp/numeric/random). – gha.st Mar 29 '15 at 22:51
  • 1
    @Domenico click on the link provided byt gha.st. It referes directly to the relevant documentation of the standard library. – Christophe Mar 29 '15 at 22:51
  • You should decide something to do when `value >= size` that isn't "write to an invalid memory address". Why is it an array of `double` instead of an array of `int`? Why did you decide not to use `std::vector`? (if that didn't even cross your mind, you really should get in the habit of thinking of using `std::vector` as your default solution to dynamically allocated arrays) –  Mar 29 '15 at 23:22

1 Answers1

0

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

Severin Pappadeux
  • 15,291
  • 3
  • 27
  • 51