Advertisement
Guest User

Untitled

a guest
Nov 21st, 2019
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.15 KB | None | 0 0
  1.  
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <math.h>
  5. #include <time.h>
  6. #define NUMBER_OF_POINTS 50000000
  7.  
  8.  
  9. void *runner (void *param);
  10.  
  11. int circle_count = 0;
  12. double random_double ()
  13. {
  14.   return random () / ((double) RAND_MAX + 1);
  15. }
  16.  
  17. int main (int argc, const *argv[])
  18. {
  19.   int thikxa =atoi(argv[1]);
  20.   int points_per_thread = NUMBER_OF_POINTS / thikxa;
  21.   int i;
  22.   double Pi;
  23.   pthread_t workers[thikxa];
  24.   srandom((unsigned)time(NULL));
  25.  
  26.   for(i=0; i<thikxa; i++)
  27.     pthread_create(&workers[i], 0, runner, &points_per_thread);
  28.   for(i=0; i<thikxa; i++)
  29.     pthread_join(workers[i], NULL);
  30.  
  31.   Pi = 4.0*circle_count/NUMBER_OF_POINTS;
  32.  
  33.   printf("NUMBER_OF_POINTS = %d\n", circle_count);
  34.   printf("Pi: = %f\n", Pi);
  35.  
  36.   return 0;
  37.  
  38.    
  39. }  
  40.  
  41. void *runner(void *param)
  42. {
  43.     int POINTS;
  44.     POINTS = *((int *)param);
  45.     int i;
  46.     int hit_count = 0;
  47.     double x,y;
  48.     for(i=0; i<POINTS; i++)
  49.     {
  50.         x=random_double()*2.0-1.0;
  51.         y=random_double()*2.0-1.0;
  52.        
  53.         if(sqrt(x*x+y*y)<1.0)
  54.             ++hit_count;
  55.     }
  56.    
  57.     circle_count += hit_count;
  58.     pthread_exit(0);
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement