mardi 24 mars 2015

Generate random number belonging to exponential distribution [duplicate]


This question already has an answer here:




I'm trying to generate random values belonging to an exponential distribution.



#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <iostream>
#include <math.h>


using namespace std;

int main(){


double x;
double random;
double one=1.0;
for(int i=0; i<1000; i++){
x = ((double) rand() / (RAND_MAX));
cout << random <<endl;
x=log(one-random)/(-3);
cout<< x<< endl;
cout<< "__"<<endl;

}



return 0;
}


I'm using this formula x = log(1-u)/(−λ) that I have read in this post Pseudorandom Number Generator - Exponential Distribution


but the output that I see is this one:



3.90272e-319
0
__
3.90272e-319
0
__
3.90272e-319
0
__
3.90272e-319
0
__
and so on


it is always printed the same value of the random number and also the result of the logarithm is always 0, I can't understand way.





Aucun commentaire:

Enregistrer un commentaire