fork download
  1. // Pi-estimating program with darts
  2. #include <stdio.h>
  3. #include<time.h>
  4. #include<stdlib.h>
  5. #include <mpi.h>
  6.  
  7. int main(){
  8.  
  9. //Seed the RNG
  10. srand(time(NULL));
  11.  
  12. //How many darts landed within the dartboard
  13. int darts_within_dartboard = 0;
  14.  
  15. //How many darts landed within the dartboard
  16. int darts_to_throw = 1e6;
  17.  
  18. for(int i =0; i < darts_to_throw; i++)
  19.  
  20.  
  21. double rand_x = ((double)rand() / RAND_MAX) * 2.0 - 1.0;
  22. double rand_y = ((double)rand() / RAND_MAX) * 2.0 - 1.0;
  23.  
  24.  
  25.  
  26.  
  27.  
  28.  
  29. double dist_squared = rand_x * rand_x + rand_y * rand_y;
  30. if(dist_squared<= 1){
  31.  
  32. darts_within_dartboard += 1;
  33. }
  34. }
  35.  
  36.  
  37. double pi_est = 4.0 * darts_within_dartboard / darts_to_throw;
  38. printf("The estimated value of pi is: %f\n", pi_est);
  39.  
  40. return 0;
  41. }
  42.  
Success #stdin #stdout #stderr 0.23s 40908KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
Error: unexpected '/' in "/"
Execution halted