fork download
  1. #include <iostream>
  2. #include <cstdlib>
  3. #include <ctime>
  4. using namespace std;
  5.  
  6. int main() {
  7.  
  8. srand(time(0));
  9.  
  10. int N = 100000;
  11. int m = 0;
  12. for (int i = 1; i <= N; i++) {
  13.  
  14. double x = (double) rand() / RAND_MAX;
  15. double y = (double) rand() / RAND_MAX;
  16.  
  17. if (x * x + y * y < 1) {
  18. m++;
  19. }
  20.  
  21. }
  22.  
  23. cout << "pi ~ " << 4.0 * m / N << endl;
  24.  
  25. return 0;
  26. }
Success #stdin #stdout 0s 4588KB
stdin
Standard input is empty
stdout
pi ~ 3.14552