dimanche 29 mars 2015

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.





Aucun commentaire:

Enregistrer un commentaire