Advertisement
MeehoweCK

Untitled

Aug 30th, 2022
521
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 long long N = 1000000000;
  8.  
  9. double pi()
  10. {
  11.     unsigned long long trafione = 0;
  12.     double x, y;
  13.  
  14.     for(unsigned long long 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.             ++trafione;
  20.     }
  21.     return 4.0 * trafione / N;
  22. }
  23.  
  24. int main()
  25. {
  26.     srand(time(nullptr));
  27.  
  28.     cout << pi() << endl;
  29.     return 0;
  30. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement