Advertisement
Eeedi

PWiR Lab3 Zad10

Mar 27th, 2018
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.28 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4. #include <omp.h>
  5.  
  6. long double fRand(long double fMin, long double fMax)
  7. {
  8.     long double f = (double)rand() / RAND_MAX;
  9.     return fMin + f * (fMax - fMin);
  10. }
  11.  
  12. int player(int playersDarts)
  13. {
  14.     srand(time(NULL));
  15.     long double pi, x, y;
  16.     int score = 0;
  17.  
  18.     for (int i = 0; i < playersDarts; i++)
  19.     {
  20.         x = fRand(-1.0, 1.0);
  21.         y = fRand(-1.0, 1.0);
  22.  
  23.         if (x*x + y*y < 1.0)
  24.             score++;
  25.     }
  26.     return score;
  27. }
  28.  
  29. void main()
  30. {
  31. long double pi;
  32. long const double REAL_PI = 3.141592653589;
  33. int score = 0, playersDarts=100000;
  34.  
  35. // devide the total number of DARTS between players
  36. double beginParallel = omp_get_wtime();
  37.  
  38. #pragma omp parallel for
  39. for (int i = 1; i <= 10; i++)
  40.     score += player(playersDarts);
  41.  
  42. double endParallel = omp_get_wtime();
  43. pi = 4.0 * ((long double)score / (long double)(10*playersDarts));
  44.  
  45. printf("\n\t Calculated pi : %.12Lf\n", pi);
  46. printf(  "\t       Real pi : %.12Lf\n", REAL_PI);
  47.  
  48. double beginSerial = omp_get_wtime();
  49. pi = 4.0 * ((long double) player(playersDarts) / (long double)playersDarts);
  50. double endSerial = omp_get_wtime();
  51.  
  52.  
  53. printf("\n\t Calculated pi : %.12Lf\n", pi);
  54. printf(  "\t       Real pi : %.12Lf\n", REAL_PI);
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement