Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <time.h>
- #include <omp.h>
- long double fRand(long double fMin, long double fMax)
- {
- long double f = (double)rand() / RAND_MAX;
- return fMin + f * (fMax - fMin);
- }
- int player(int playersDarts)
- {
- srand(time(NULL));
- long double pi, x, y;
- int score = 0;
- for (int i = 0; i < playersDarts; i++)
- {
- x = fRand(-1.0, 1.0);
- y = fRand(-1.0, 1.0);
- if (x*x + y*y < 1.0)
- score++;
- }
- return score;
- }
- void main()
- {
- long double pi;
- long const double REAL_PI = 3.141592653589;
- int score = 0, playersDarts=100000;
- // devide the total number of DARTS between players
- double beginParallel = omp_get_wtime();
- #pragma omp parallel for
- for (int i = 1; i <= 10; i++)
- score += player(playersDarts);
- double endParallel = omp_get_wtime();
- pi = 4.0 * ((long double)score / (long double)(10*playersDarts));
- printf("\n\t Calculated pi : %.12Lf\n", pi);
- printf( "\t Real pi : %.12Lf\n", REAL_PI);
- double beginSerial = omp_get_wtime();
- pi = 4.0 * ((long double) player(playersDarts) / (long double)playersDarts);
- double endSerial = omp_get_wtime();
- printf("\n\t Calculated pi : %.12Lf\n", pi);
- printf( "\t Real pi : %.12Lf\n", REAL_PI);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement