MeehoweCK

Untitled

Oct 9th, 2020
1,048
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. double pi(unsigned N)
  8. {
  9.     double x, y;
  10.     unsigned n = 0;
  11.  
  12.     for(unsigned i = 0; i < N; ++i)
  13.     {
  14.         x = 1.0 * (rand() - 1) / 32766;
  15.         y = 1.0 * (rand() - 1) / 32766;
  16.  
  17.         if(x * x + y * y <= 1)
  18.             ++n;
  19.     }
  20.  
  21.     return 4.0 * n / N;
  22. }
  23.  
  24. int main()
  25. {
  26.     srand(static_cast<unsigned>(time(nullptr)));
  27.  
  28.     cout << pi(100000000) << endl;
  29.     return 0;
  30. }
Advertisement
Add Comment
Please, Sign In to add comment