Advertisement
MeehoweCK

Untitled

Oct 17th, 2022
883
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.48 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdlib>
  3. #include <ctime>
  4.  
  5. using namespace std;
  6.  
  7. const unsigned N = 1000000000;
  8.  
  9. double szacowanie_pi()
  10. {
  11.     double x, y;
  12.     unsigned n = 0;
  13.  
  14.     for(unsigned i = 0; i < N; ++i)
  15.     {
  16.         x = 1.0 * rand() / RAND_MAX;
  17.         y = 1.0 * rand() / RAND_MAX;
  18.         if(x * x + y * y <= 1)
  19.             ++n;
  20.     }
  21.     return 4.0 * n / N;
  22. }
  23.  
  24. int main()
  25. {
  26.     srand(time(nullptr));
  27.     cout << szacowanie_pi() << endl;
  28.     return 0;
  29. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement