Advertisement
luizaspan

MC Estimativa Pi (integrando por números aleatórios)

Nov 13th, 2015
373
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.49 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4. #include <math.h>
  5.  
  6. #define FRANDOM ((double) random()/RAND_MAX)
  7.  
  8. #define N 1e6 // não poderia inicializar vetor
  9.  
  10. int main(void)
  11. {
  12.     double x, y, nacertos;
  13.  
  14.     for (double n=1; n<N; n*=2) {
  15.  
  16.         nacertos = 0.0;
  17.  
  18.         for (int i = 0; i < N; ++i)
  19.         {
  20.             x = FRANDOM;
  21.             y = FRANDOM;
  22.  
  23.             double f = x*x + y*y;
  24.  
  25.             if (f<1)
  26.                 nacertos++;
  27.         }
  28.  
  29.     printf("%lf\t%lf\n",n,(nacertos/N)*4); // se não 1e6 -> (double) N
  30. }
  31.  
  32. return 0;
  33. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement