Advertisement
MeehoweCK

Untitled

Mar 9th, 2023
531
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.65 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.    
  11.     unsigned n = 0;
  12.    
  13.     for(unsigned i = 0; i < N; ++i)
  14.     {
  15.         x = 1.0 * rand() / RAND_MAX;
  16.         y = 1.0 * rand() / RAND_MAX;
  17.        
  18.         if(x * x + y * y <= 1)
  19.             ++n;
  20.     }
  21.    
  22.     return 4.0 * n / N;
  23. }
  24.  
  25. int main()
  26. {
  27.     srand(time(nullptr));
  28.     unsigned N;
  29.     cout << "Podaj liczbe ile punktow ma byc losowanych (im wiecej punktow, tym wyzsza dokladnosc szacowania): ";
  30.     cin >> N;
  31.     cout << "Liczba PI zostala oszacowana na ";
  32.     cout << pi(N);  
  33.    
  34.     return 0;
  35. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement