/* compile with gcc -lm exp.c test with a.out 1000 2 > test.dat then run R and examine with test_scan("test.dat") hist(test) */
#include <stdio.h> #include <stdlib.h> #include <math.h>
float urand() { return( (float) rand()/RAND_MAX ); }
float genexp(float lambda) { float u,x; u=urand(); x=(-1/lambda)*log(u); return(x); }
void main(int argc, char *argv[]) { float e,lambda; int i,n; n=atoi(argv[1]); lambda=atof(argv[2]); for (i=0;i<n;i++) { e=genexp(lambda); printf(" %2.4f \n",e); } }
|