Skip to content

Instantly share code, notes, and snippets.

@ihodes
Created August 20, 2010 05:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ihodes/539670 to your computer and use it in GitHub Desktop.
Save ihodes/539670 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
int main(int argc, char **argv){
long iterations = atoi(argv[1]);
double sum = 0;
long ans, i, j = 0;
long seed = time((time_t *) NULL);
srand48(seed);
for(i = 0; i <= iterations; i++){
for(j = 1, sum = 0; (sum += drand48()) <= 1.0; j++);
ans += j;
}
printf("Our estimate for e is %.10f.\n", (double) ans/iterations);
return 1;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment