Advertisement
MeehoweCK

Untitled

Jan 18th, 2021
994
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.50 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 szacuj_pi()
  10. {
  11.     unsigned n = 0;
  12.     double x, y;
  13.  
  14.     srand(time(nullptr));
  15.  
  16.     for(unsigned i = 0; i < N; ++i)
  17.     {
  18.         x = 1.0 * (rand() - 1) / (RAND_MAX - 1);
  19.         y = 1.0 * (rand() - 1) / (RAND_MAX - 1);
  20.         if(x * x + y * y <= 1)
  21.             ++n;
  22.     }
  23.  
  24.     return 4.0 * n / N;
  25. }
  26.  
  27. int main()
  28. {
  29.     cout << szacuj_pi() << endl;
  30.     return 0;
  31. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement