pi.c 520 B

12345678910111213141516171819202122232425
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. int main(int argc, char *argv[])
  4. {
  5. int i, its, hits = 0;
  6. double d1, d2;
  7. if (argc != 2) {
  8. fprintf(stderr, "Usage: %s <iterations>\n", argv[0]);
  9. exit(0);
  10. }
  11. its = atoi(argv[1]);
  12. srandom(1);
  13. for (i = 0; i < its; i++) {
  14. d1 = ((double)random())/2147483647.0;
  15. d2 = ((double)random())/2147483647.0;
  16. if (((d1*d1) + (d2*d2)) <= 1)
  17. hits++;
  18. }
  19. printf("%.10f\n", (double)4.0 * (double) ((double)hits / (double)its));
  20. return 1;
  21. }