fork download
  1. #include <iostream>
  2. #include <cstdlib>
  3. #include <ctime>
  4. using namespace std;
  5. int main() {
  6. srand(time(0));
  7. int N = 1000;
  8. int count = 0;
  9.  
  10. for(int i = 0; i < N; i++) {
  11. double x = (double)rand()/RAND_MAX;
  12. double y = (double)rand()/RAND_MAX;
  13. if(x*x + y*y <= 1) count++;
  14. }
  15. double pi = 4.0 * count / N;
  16. cout << "Estimated Pi = " << pi << endl;
  17. return 0;
  18. }
  19.  
Success #stdin #stdout 0.01s 5288KB
stdin
Standard input is empty
stdout
Estimated Pi = 3.104